簡體   English   中英

Bcrypt 沒有將散列密碼分配給 body.password 變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM