ita*_*ish 1 javascript postgresql orm this sequelize.js
我正在尝试使用关键字访问存储在用户实例中的密码this,但它不起作用?
这是我打电话时的代码.
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));
Run Code Online (Sandbox Code Playgroud)
这是我的实例方法的代码.
instanceMethods :{
authenticate: (password, cb) => {
return bcrypt.compare(password, this.password, (err, match) => {
if (err) return cb(new errors.Unauthorized());
return cb(null, match);
});
}
}
Run Code Online (Sandbox Code Playgroud)
你使用箭头函数,它不允许this通过call()或绑定apply().见MDN:
由于这不受箭头函数的约束,因此call()或apply()方法只能传入参数.这被忽略了.
并进一步,
如前所述,箭头函数表达式最适合非方法函数......
请参阅此github问题以及https://github.com/sequelize/sequelize/issues/5858
没有办法覆盖箭头功能的范围 - 这是他们的主要功能之一.所以我们在这里无能为力