简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z '模型不是构造函数' 错误

[英]Mongoose 'model is not a constructor' error

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.最常见的错误是使用module.export而不是module.exports并调用架构的新实例而不是 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在这里let user = new User({我得到TypeError: User is not a constructor

I remember having a similar issue with webpack/typescript.我记得 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.就我而言,webpack 所做的事情弄乱了进口。

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 ).另一个更新:我尝试了您的示例代码,它对我有用(我还使用最新的 Mongo 版本进行了测试,它仍然有效)。 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.在您的示例中,我唯一需要更改的是模块导出行,您只导出了 MK 而不是用户 model。

module.exports = {User, SigningKey, MK};

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM