user.isModified is not a function when doing a pre update in mongoose
mongodb, mongoose, node.js, schema
Solution
You get an error because the arrow function changes the scope of 'this.' Just use
UserSchema.pre('save', function(next){})
Problem
I'm trying to hash passwords in an app I'm building and they are hashing fine when I create a user by calling this function(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 ``` but when I do an update and have this pre: ``` UserSchema.pre 'findOneAndUpdate', (next) -> user = this hashPass(user, next) ``` I get `TypeError: user.isModified is not a function` if I console log user in the pres the save pre logs the user I am updating, the findandupdate pre does not,id there to way to access the document in the pre or do I need to do this another way? NOTE: The above functions are NOT arrow functions. They are normal functions written in CoffeeScript with ->, they compile into js with normal functions.