I need to reuse one of pre middleware functions in another pre middleware, for that I extracted a function like so:
async function encryptPassword(next) {
if (!this.isModified('password')) {
return next();
}
this.password = await bcrypt.hash(this.password, 5);
this.passwordChangedAt = new Date();
next();
}
UserSchema.pre('save', encryptPassword);
UserSchema.pre("findOneAndUpdate", encryptPassword);
But I am getting an error saying that this.isModified
is not a function, I assume that this
is referring to something else. How to fix this?