So this seems like a fairly popular error. And after seeing all the previous questions, I am posting this as they don't solve my problem. Most popular errors were using module.export
instead of module.exports
and calling a new instance of the schema instead of the model. I am making none of these mistakes. Here's the code.
const db = require('mongoose'); db.Promise = global.Promise; db.connect(process.env.MONGODB_URI || 'mongodb://localhost/mydb').then( () => {console.log('Connection to database established')} ).catch((err) => { console.log(err); }); module.exports = {db};
const {db} = require('./dbconf'); // Schema for users let UserSchema = new db.Schema({ username: { type: String, required: true, trim: true, min: 1, unique: true }, password: { type: String, required: true, trim: true, min: 8 }, name: { type: String, required: true, trim: true, min: 1 }, email: { type: String, required: true, trim: true, min: 1, unique: true } }); const User = db.model('User', UserSchema); module.exports = {User};
const {User} = require('../db/models'); let user = new User({ username: username, password: password, name: name, email: email });
And here at let user = new User({
I get TypeError: User is not a constructor
I remember having a similar issue with webpack/typescript.
Try exporting the module like
module.exports.User = User;
and import the entire db index file without using destructor...just to see if it makes a difference.
const models = require('../db/models');
const user = new models.User(...);
In my case it was something that webpack did that messed up the imports.
Quick Edit: I created the same setup as you have(with the latest mongoose).
// models file
const userSchema = new Schema({
message: { type: String, maxlength: 5000 }
});
const User = mongoose.model('User', userSchema);
module.exports.User = User;
// another file
const { User } = require('./testmodels.js');
console.log('==>', User );
will log out
==> function model(doc, fields, skipId) {
model.hooks.execPreSync('createModel', doc);
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
const discriminatorKey = model.schema.options.discriminatorKey;
if (model.discriminators == null || doc == null || doc[discriminatorKey] == null) {
Model.call(this, doc, fields, skipId);
return;
}
// If discriminator key is set, use the discriminator instead (gh-7586)
const Discriminator = model.discriminators[doc[discriminatorKey]] ||
getDiscriminatorByValue(model, doc[discriminatorKey]);
if (Discriminator != null) {
return new Discriminator(doc, fields, skipId);
}
// Otherwise, just use the top-level model
Model.call(this, doc, fields, skipId);
}
Just out of curiosity, what happens if you try to call the constructor like this.
let user = new User.User({
username : username,
password : password,
name : name,
email : email
});
Another update: I tried out your example code and it works for me( I also tested with the latest Mongo version and it still worked ). The only thing I had to change in your example was the module exports line where you only exported MK and not the User model.
module.exports = {User, SigningKey, MK};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.