【发布时间】:2021-02-03 21:30:51
【问题描述】:
我有这段代码,我试图用 mongoose virtual 和 mongoose 方法将普通密码设置为加密密码 这是我的代码
UserSchema.virtual("password")
.set(async function (password) {
this.encry_password = this.returnSecurePassword(password)
})
.get(function () {
return this.encry_password;
});
UserSchema.methods = {
returnSecurePassword: async function (password) {
const saltRounds = 10
await bcrypt.hash(password, saltRounds, function await(err, hash) {
console.log(hash);
return hash;
});
},
};
但每当我运行它时,它都会显示承诺待处理
【问题讨论】:
-
returnSecurePassword是一个承诺吗?如果是这样,你错过了await。此外,function await(err, hash)也不正确。您必须删除await。 -
@Jérôme 是的,我保证你能进一步解释一下
标签: javascript node.js mongodb asynchronous mongoose