I am facing a weird issue.
The below code throws error and it uses arrow function
UserSchema.pre('save', next => {
const SALT_FACTOR = 5;
if (!this.isModified('password')) return next();
bcrypt.genSalt(SALT_FACTOR, (err, salt) => {
if (err) return next(err);
bcrypt.hash(this.password, salt, null, (err, hash) => {
if (err) return next(err);
this.password = hash;
next();
});
});
});
The below code works fine and it uses normal function.
UserSchema.pre('save', function(next) {
const SALT_FACTOR = 5;
if (!this.isModified('password')) return next();
bcrypt.genSalt(SALT_FACTOR, (err, salt) => {
if (err) return next(err);
bcrypt.hash(this.password, salt, null, (err, hash) => {
if (err) return next(err);
this.password = hash;
next();
});
});
});
Anyone has an idea why arrow function throws below error?
TypeError: this.isModified is not a function