在预保存挂钩中调用异步函数正在返回我undefined
密码。我是否从根本上误解了async
这里?我已经在我的应用程序的其他区域成功使用它,它似乎工作正常......
userSchema.pre('save', function (next) {
let user = this
const saltRounds = 10;
const password = hashPassword(user)
user.password = password;
next();
})
async hashPassword(user) {
let newHash = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
console.log(err)
}
return hash
});
return newHash
}
我认为你需要处理 hashPassword 返回的承诺:
hashPassword(user)
.then(password => {user.password = password
next()}
)
我不认为你可以将 userSchema.pre 变成异步函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)