Mongoose:schema._getVirtual不是一个函数

我突然遇到麻烦(在我更新Mongoose包版本之前工作正常)。 目前使用Mongoose 4.7.6。

var userSchema = require('schemas/user'), User = db.model('User', userSchema); // db is already known User .findById(user._id) // user is already known due to auth .populate('group currentPlayer') .exec(function (findErr, userPlayer) { ... }); 

如果我的用户架构是必要的,我会发布,但目前还没有由于其长度(虚拟,方法,静态)。

错误:

 /app/node_modules/mongoose/lib/model.js:2986 var virtual = modelForCurrentDoc.schema._getVirtual(options.path); ^ TypeError: modelForCurrentDoc.schema._getVirtual is not a function at getModelsMapForPopulate (/app/node_modules/mongoose/lib/model.js:2986:49) at populate (/app/node_modules/mongoose/lib/model.js:2660:15) at _populate (/app/node_modules/mongoose/lib/model.js:2628:5) at Function.Model.populate (/app/node_modules/mongoose/lib/model.js:2588:5) at Immediate.<anonymous> (/app/node_modules/mongoose/lib/query.js:1275:17) ... 

用户架构

 var Mongoose = require('mongoose'), Bcrypt = require('bcrypt'), ObjectID = Mongoose.Schema.Types.ObjectId, UserSchema = new Mongoose.Schema({ active : { type: Boolean, default: true }, created : { type: Date, required: true, default: Date.now }, modified : { type: Date, required: true, default: Date.now }, createdBy : { type: ObjectID, ref: 'User' }, modifiedBy : { type: ObjectID, ref: 'User' }, email : { type: String, required: true }, salt : { type: String }, hash : { type: String }, session : String, group : { type: ObjectID, ref: 'Group', required: true }, currentPlayer : { type: ObjectID, ref: 'Player' }, validated : { type: Boolean, default: false }, ipAddress : String, lastIp : String, notes : String }); var _checkPassword = function (password) { ... }; UserSchema.pre('validate', function (next) { if (this.password && !_checkPassword(this.password)) { this.invalidate('password', 'invalid', "Six character minimum, must contain at least one letter and one number or special character."); } next(); }); UserSchema.pre('save', function (next) { this.modified = Date.now(); next(); }); UserSchema.virtual('password') .get(function () { return this._password; }) .set(function (passwd) { this.salt = Bcrypt.genSaltSync(10); this._password = passwd; this.hash = Bcrypt.hashSync(passwd, this.salt); }); UserSchema.method('verifyPassword', function (password, done) { Bcrypt.compare(password, this.hash, done); }); UserSchema.static('authenticate', function (email, password, done) { ... }); module.exports = UserSchema; 

如果有人遇到这个问题,这可能是因为你有两个包含mongoose的多个package.json文件作为依赖。 确保在项目中使用一个软件包版本的mongoose,并在那里注册模型。

我现在已经在GitHub上提交了一个bug,因为恢复到4.6.8版本允许我的应用程序再次工作。 https://github.com/Automattic/mongoose/issues/4898

升级到Mongoose v4.7后,我现在在填充文档时收到一个错误。

重现这一错误的一系列事件:

 - define a Schema in its own file and use module.exports on the defined Schema object - require() the Schema file - use mongoose.model() to build a model from this Schema - attempt to retrieve a record by using find() and populate() - TypeError: modelForCurrentDoc.schema._getVirtual is not a function 

如果我不使用“外部”架构文件,而是定义内联架构,问题就会消失。 然而,由于在许多模式中定义的静态,方法和虚拟,这不是成立的。