I've create a Mongoose model and I wrote some code to test if it works, I tried to save the model and then display all of the models but it doesn't log anything to the console and the model isn't saving.
My testing code:
let User = require('./models/user')(MAI);
let myUser = new User({
username: 'Admin',
email: 'admin@example.com',
password: '123456',
});
await myUser.save();
User.find((err, users) => {
if(err) console.log(err);
console.dir(users);
});
My model:
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
user_id: Number,
username: String,
email: String,
password: String,
verified: { type: Boolean, default: false },
joindate: { type: Date, default: Date.now },
postcount: { type: Number, default: 0 },
});
module.exports = function(MAI) {
UserSchema.plugin(MAI.plugin, {
model: 'User',
field: 'user_id',
startAt: 1,
incrementBy: 1,
unique: true
});
return mongoose.model('User', UserSchema);
}
And if it's important, mongoose & mai initialization:
mongoose.Promise = require('bluebird');
let connection = mongoose.createConnection('mongodb://localhost:27017/forum');
MAI.initialize(connection);
Fixed it, the problem was I required mongoose in the model file when what I should've done was to pass the mongoose from my entry point.