Mongoose bcryptjs比较密码不参考文件(这个)

我有像这样的mongoose模式

var mongoose = require ('mongoose'); var bcrypt = require('bcryptjs'); var Schema = mongoose.Schema; var SALT_WORK_FACTOR = 10; var touristSchema = new Schema ({ local: { email: String, password: String }, facebook: { id: String, token: String, email: String, name: String, } }); touristSchema.pre('save', function(next) { var user = this; console.log('bcrypt called by strategy', user); // if user is facebook user skip the pasword thing. if (user.facebook.token) { next(); } // only hash the password if it has been modified (or is new) if (!user.isModified('password') && !user.isNew){ console.log('I am in here', user.isNew); return next(); } // generate a salt bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { console.log('I am in genSalt'); if (err) return next(err); // hash the password using our new salt bcrypt.hash(user.local.password, salt, function(err, hash) { if (err) return next(err); // override the cleartext password with the hashed one user.local.password = hash; next(); }); }); }); touristSchema.methods.comparePassword = function(candidatePassword, cb) { bcrypt.compare(candidatePassword, this.local.password, function(err, isMatch) { // console.log(this.local.password); if (err) return cb(err); cb(null, isMatch); }); }; module.exports = mongoose.model('users', touristSchema); 

和这样的login策略

 passport.use('local-login', new LocalStrategy({ usernameField: 'email', passwordField: 'password', passReqToCallback: true }, function(req, email, password, done) { process.nextTick(function() { User.findOne({ 'local.email': email }, function(err, user) { if(err) return done(err); if(!user) return done(null, false, req.flash('loginMessage', 'No User Found')); user.comparePassword(password, function(err, isMatch) { if (err) throw err; if (isMatch) { done(null, user); } else done(null, false, req.flash('loginMessage', 'Incorrect password')); }); }); }); } )); 

一切都按预期工作,但当我尝试login它给我错误: TypeError:无法读取未定义的属性'密码' 。 原来this.local是未定义的。 这是非常奇怪的,因为在touristSchema.pre钩子我们分配了var user = this并logging这个返回的用户文档。 在比较方法中我们使用的是touristSchema.methods.compare那么this也应该参考touristSchema.methods.compare中间件文档中写的文件。 现在我已经打了好几天了,并且已经消耗了所有可能的帮助。 任何帮助提前非常感谢。 谢谢,是的,我的
mongnodb版本是v3.2.15
mongoose版本是4.9.7
根据mongoose的文档,这看起来与我兼容