【发布时间】:2021-09-26 04:10:45
【问题描述】:
尝试实施重置密码功能,但这样做时不断收到UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string 错误。我在其他项目中使用过这段代码,它完美无瑕,但由于某种原因,我似乎无法弄清楚为什么它会不断弹出或下面的代码可能有什么问题..
方法:
UserSchema.methods.hashPassword = () => {
return new Promise((resolve, reject) => {
bcrypt.genSalt(10, (err1, salt) => {
if (err1) {
reject(err1);
}
bcrypt.hash(this.password, salt, (err2, hash) => { // <-- it appears to happen on this line at ".hash"
if (err2) {
reject(err2);
}
this.password = hash;
resolve(hash);
});
});
});
};
路线:
const resetPassword = async (req, res) => {
// Update User
const { password } = req.body;
user.password = password;
user.passwordResetToken = '';
user.passwordResetExpires = moment().utcOffset(0);
user.hashPassword().then(() => { <---- Happens here
// Save Updated User to the Database
user.save((error) => {
if (error) {
return res.status(500).json({
success: false,
message: 'An unexpected error occurred.',
});
}
// Send Email Confirming Password Change to the User
});
});
});
});
};
【问题讨论】:
-
可能会回调。并返回完成会解决这个问题?
标签: node.js express mongoose bcrypt