问题描述
我正在用 Typescript 编写一个 Express 端点来更新数据库中用户的密码,但在处理多个查询/同步代码时遇到了一些问题。
我的问题是现在我正在尝试等待函数的返回,但它似乎没有等待。
我的路线如下所示:
router.put("/:userId",validateUser,async (req: any,res: Response) => {
const result: any = await updatePassword(req.body.password,req.body.confirmPassword,req.params.userId);
console.log(result) //LOGS UNDEFINED????
if (result.status !== 200) res.status(result.status).send({ message: result.message,code: result.code });
res.send("Success!");
});
那个 updatePassword
函数看起来像这样:
const updatePassword = async (password: string,confirmPassword: string,userId: string) => {
if (password !== confirmPassword) {
return { status: 409,message: Errors.mismatchingPasswordMessage,code: Errors.mismatchingPasswordCode };
}
const hashedPassword = bcrypt.hashSync(password,10);
//update the password in the DB
pool.query("UPDATE users SET password = ? WHERE userId = ?",[hashedPassword,userId],(err: MysqlError,result: any) => {
if (err) {
console.log(err);
return { status: 500,message: err.message,code: err.code };
}
if (result.changedRows !== 1) {
return { status: 500,message: "Password could not be updated!" };
}
return { status: 200 };
});
}
为什么这条路线没有正确等待updatePassword
的结果??
在此先感谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)