Mongoose:schema._getVirtual不是函数

时间:2017-01-13 19:43:00

标签: node.js mongoose mongoose-schema mongoose-populate

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我现在已经在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

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

答案 2 :(得分:0)

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

  

引发错误是因为我有一个名为id的字段,可能是   覆盖内部的_id字段。

来源:https://stackoverflow.com/a/53569877/5683645