[英]Bcrypt didn't assign the hashed password to body.password variable
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) 散列密碼時,它成功地將散列密碼記錄到安慰。 是否有任何問題導致這種情況?
module.exports.handle_sign_up = async (req,res) => {
let body = req.body
let hashedPassword;
try{
hashedPassword = await bcrypt.hash(body.password,10);
body.password = hashedPassword;
console.log(hashedPassword,body.password);
}catch(err){
console.log(err)
})
res.send(body)
};
try and catch
一種方法 module.exports.handle_sign_up = async (req,res) => {
let body = req.body
bcrypt.hash(body.password,10)
.then((hashedPassword) => {
body.password = hashedPassword;
})
.catch(err => console.log(err))
res.send(body)
};
await
返回一個承諾,如果你不存儲它,它就會作為一個沒有解決的承諾then and catch
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.