在mongoose进行预更新时,user.isModified不是函数

我正在尝试哈希密码在我正在build设的应用程序,他们哈希很好,当我创build一个用户通过调用这个函数(coffeesctipt):

UserSchema.pre 'save', (next) -> user = this hashPass(user, next) hashPass = (user, next) -> # only hash the password if it has been modified (or is new) if !user.isModified('password') return next() # generate a salt bcrypt.genSalt SALT_WORK_FACTOR, (err, salt) -> if err return next(err) # hash the password using our new salt bcrypt.hash user.password, salt, (err, hash) -> if err return next(err) # override the cleartext password with the hashed one user.password = hash next() return return 

但是当我做一个更新,并有这个前:

 UserSchema.pre 'findOneAndUpdate', (next) -> user = this hashPass(user, next) 

我得到TypeError: user.isModified is not a function如果我控制台login用户在压力保存预先login我更新的用户,findandupdate pre不,id有方法来访问文件中的pre或我需要以另一种方式做到这一点?

我在打字稿上有类似的问题,事实certificate,这与您正在使用的箭头操作符有关。 不知道如何改变这个在coffescript现在,但我认为这应该解决您的问题。

你必须改变这一行:

 hashPass = (user, next) -> 

看看这个: https : //github.com/Automattic/mongoose/issues/4537