[英]Sequelize: user.checkPassword is not a function
I'm trying to create a prototype function inside sequelize model, to validade my password:我正在尝试在 sequelize 模型中创建一个原型函数,以验证我的密码:
const bcrypt = require('bcryptjs');
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
'User',
{
name: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.VIRTUAL,
password_hash: DataTypes.STRING
},
{
hooks: {
beforeSave: async user => {
if (user.password) {
user.password_hash = await bcrypt.hash(user.password, 8);
}
}
}
}
);
**User.prototype.checkPassword = function(password) {
debugger
return bcrypt.compare(password, this.password_hash);
};**
return User;
};
but when I export this model to my controller and try do call user.checkpassword, it gaves me TypeError: user.checkPassword is not a function :但是当我将此模型导出到我的控制器并尝试调用 user.checkpassword 时,它给了我TypeError: user.checkPassword is not a function :
const { User } = require('../models');
class SessionController {
async store(req, res) {
const { email, password } = req.body;
const user = User.findOne({ where: { email } });
if (!user) {
return res.status(401).json({ message: 'User not found' });
}
**if (!(await user.checkPassword(password))) {
return res.status(401).json({ message: 'Incorret password' });
}**
return res.status(200).send();
}
}
module.exports = new SessionController();
what can it be??会是什么??
my sequelize version is "sequelize": "^5.21.5"我的续集版本是“续集”:“^5.21.5”
User.findOne({ where: { email } });
returns a promise.返回一个承诺。 You probably meant to do
你可能打算这样做
const user = await User.findOne({ where: { email } });
instead of代替
const user = User.findOne({ where: { email } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.