Search code examples
node.jsmongoosemongoose-schemamongoose-populate

Mongoose: schema._getVirtual is not a function


I'm having trouble with population all of a sudden (was working fine before I updated Mongoose package version). Currently using 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) { ... });

If my Schema for User is necessary, I will post it, but currently haven't due to its length (virtuals, methods, statics).

Error:

/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)
...

User Schema

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;

Solution

  • If anyone comes across this problem, it is probably because you have multiple package.json files with mongoose as a dependency in two of them. Make sure you use one package version of mongoose in your project and register your models there.