I'm trying to get access to the password stored in the user instance using keyword this and it doesn't work?
Here is the code when I'm calling it.
Users.find({where: {$or: [{ email: email} ,{ username: username }]}})
.then(user => {
if (!user) return res.status(401).send(new errors.Unauthorized('Email/Username or Password Not Found!'));
user.authenticate(password, (err, match) => {
if (err) return res.status(401).send(err);
res.send(user);
});
})
.catch(err => res.send(err));
Here is the code for my instance method.
instanceMethods :{
authenticate: (password, cb) => {
return bcrypt.compare(password, this.password, (err, match) => {
if (err) return cb(new errors.Unauthorized());
return cb(null, match);
});
}
}
You use arrow function, which doesn't let this to be bound via call() or apply(). See MDN:
Since this is not bound in arrow functions, the methods call() or apply() can only pass in parameters. this is ignored.
And further,
As stated previously, arrow function expressions are best suited for non-method functions...
See this github issue as well https://github.com/sequelize/sequelize/issues/5858
There is no way to override the scope of arrow functions - that's one of their main features. So there's nothing we can do here