8
我有一个简单的用户模型如下:更新前似乎并没有被称为
'use strict';
let hashPassword = (user, options) => {
if (!user.changed('password')) { return; }
return require('bcrypt')
.hash(user.getDataValue('password'), 10)
.then(hash => user.setDataValue('password', hash));
};
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
username: {allowNull: false, type: DataTypes.STRING, unique: true},
email: {allowNull: false, type: DataTypes.STRING, unique: true},
password: {allowNull: false, type: DataTypes.STRING, unique: false},
}, {
hooks: {
beforeCreate: hashPassword,
beforeUpdate: hashPassword
}
});
return User;
};
它工作得非常好用户创建,但beforeUpdate
钩似乎不工作或叫,密码以纯文本形式保存在数据库中。
它从哪里来,它如何被固定?
看起来你是对的,我使用'db.user.update(...,{where:{id:req.params.userId}});'。非常感谢你 – Magix
很高兴我能提供帮助。如果我回答了您的问题,请标记为已接受。 –
你可以发布示例代码,因为我找不到实际参数 –