I am updating my Express.js Application to be fully compliant with the Google Javascript Style Guide. The pre-save method on my Mongoose.js user schema to hash passwords refers to itself using this to get the password to hash though that conflicts with section 5.9 of the Google Javascript Style Guide. How do I adjust the pre-save method to avoid using this and become compliant with section 5.9?
Code
UserSchema.pre('save', (next) => {
bcrypt.hash(this.password, 10, (err, hash) => {
if (err) {
return next(err);
}
this.password = hash;
next();
});
});
Google Javascript Style Guide Requirement
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.
https://google.github.io/styleguide/jsguide.html#features-this