【发布时间】:2018-07-25 18:37:47
【问题描述】:
从this question开始。
我觉得我快到了,但我对异步的不完全理解使我无法解决这个问题。我基本上只是尝试使用 bcrypt 对密码进行哈希处理,并决定分离 hashPassword 函数,以便我可以在应用程序的其他部分使用它。
hashedPassword 不断返回 undefined ......
userSchema.pre('save', async function (next) {
let user = this
const password = user.password;
const hashedPassword = await hashPassword(user);
user.password = hashedPassword
next()
})
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
return err;
}
return hash
});
return hashedPassword
}
【问题讨论】:
标签: javascript node.js mongoose bcrypt