【发布时间】:2021-02-27 09:23:13
【问题描述】:
module.exports.handle_sign_up = async (req,res) => {
let body = req.body
await bcrypt.hash(body.password, 10, (err,hash) => {
body.password = hash
console.log(hash)
})
res.send(body)
};
上面是我的代码,它使用 bcrypt 对 body.password 进行哈希处理。我试图在回调函数中将散列密码分配给 body.password 但是当 res.send(body) 执行时它返回未散列的密码,同时当我尝试 console.log(hash) 散列密码时,它成功地将散列密码记录到安慰。有什么问题导致这个吗?
【问题讨论】:
-
大多数
async and await的初学者都会犯同样的错误,你需要存储promise来解决或拒绝它。