【发布时间】:2018-03-28 01:41:27
【问题描述】:
使用 Passport 和 mongoose,我正在尝试对保存到数据库的密码进行哈希处理,但它根本没有发生。
我已经定义了模型,并且我可以确认对象正在写入数据库,所以它必须在我的预保存中间件定义的块中。
UserSchema.pre('save', (next) => {
console.log('Pre-Save Hash has fired.')
let user = this;
bcrypt.genSalt(10, (err, salt) => {
if (err) console.error(err);
bcrypt.hash(user.password, salt, (err, hash) => {
user.password = hash;
next();
});
});
});
不知道为什么当我清楚地有东西时它以明文形式保存密码字段。它可能执行不正确。 (中间件中的 console.log 确实会记录到控制台。)
【问题讨论】:
标签: mongoose