I have been updating an applications code to be 100% compliant with Google’s Javascript Style Guide and have come across an issue I’m unsure how to adjust my code to be complaint. More information is below, any help would be appreciated
Mongoose.js User Schema password hashing code:
This pre-insert function ensures the user’s password is hashed with bcrypt. However due to it refering to itself (this) it fails to meet the “no-invalid-this” requirement.
UserSchema.pre('save', (next) => {
bcrypt.hash(this.password, 10, (err, hash) => {
if (err) {
return next(err);
}
this.password = hash;
next();
});
});
50:18 error Unexpected 'this' no-invalid-this
5.9 this
Only use this in class constructors and methods, or in arrow functions defined within class constructors and methods. Any other uses of this must have an explicit @this declared in the immediately-enclosing function’s JSDoc.Never use this to refer to the global object, the context of an eval, the target of an event, or unnecessarily call()ed or apply()ed functions.
Source: Google JavaScript Style Guide