0

Here's a piece of perfectly-working script in my project:

User.pre('save', function (next) {
  const user = this;
  if (this.isModified('password') || this.isNew) {
    bcrypt.genSalt(10, function (err, salt) {
      if (err) {
        return next(err);
      }
      bcrypt.hash(user.password, salt, function (err, hash) {
        if (err) {
          return next(err);
        }
        user.password = hash;
        next();
      });
    });
  } else {
    return next();
  }
});

But since my linter didn't like it, I converted it to a fat-arrow format:

User.pre('save', (next) => {
  const user = this;
  if (this.isModified('password') || this.isNew) {
    bcrypt.genSalt(10, (err, salt) => {
      if (err) {
        return next(err);
      }
      bcrypt.hash(user.password, salt, (err, hash) => {
        if (err) {
          return next(err);
        }
        user.password = hash;
        next();
      });
    });
  } else {
    return next();
  }
});

But now the same script refuses to recognize the isModified method!?

TheLearner
  • 2,335
  • 2
  • 29
  • 69
  • No. It refuses to recognise `user = this`. Don't use arrow functions where a method is required. – Bergi Sep 04 '17 at 12:12
  • The term "fat arrow function" has not been used for at least two years. –  Sep 04 '17 at 12:42

0 Answers0