猫鼬:schema._getVirtual 不是函数

Mongoose: schema._getVirtual is not a function

我突然遇到人口问题(在我更新 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) { ... });

如果我的用户模式是必需的,我会post它,但由于它的长度(虚拟、方法、静态)目前还没有。

错误:

/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;

我现在已经在 GitHub 上提交了一个错误,因为恢复到版本 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

如果我不使用 "external" 模式文件,而是定义内联模式,问题就会消失。然而,由于许多模式中定义的静态、方法和虚拟,这是站不住脚的。

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

A 可能的答案可以在这里找到:

The error was raised because I had a field called id that probably was overriding the internal _id field.

来源: