我正在开发一个带有nodejs/express/sequelize的API。
实际上,当我创建一个用户帐户时。我得到了一个钩子'beforeCreate‘,它通过bcrypt散列用户密码:
User.hook('beforeCreate', (user) => {
if (user.password) {
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
}
});其中user.password是我注册时提供的密码。
这部分工作得很好。
其次,我有一个功能,更新这个用户帐户。逻辑上,我添加了一个引擎盖'beforeUpdate‘:
User.hook('beforeUpdate', (user) => {
console.log(user);
if (user.password) {
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
}
});但是我遇到了一个问题,因为user.password不是我在补丁请求中提供的数据,而是我数据库中的散列密码。
我想是模型密码而不是实例密码.我不知道。如果有人能帮我得到查询值,那就太好了。
发布于 2018-06-04 04:42:04
您可以使用_previousDataValues
User.hook('beforeUpdate', (user) => {
console.log(user);
if (user.password) {
user.password = bcrypt.hashSync(user.previous.password, bcrypt.genSaltSync(10), null);
}
});https://stackoverflow.com/questions/50648024
复制相似问题