簡體   English   中英

為什么 hashedPassword = await bcrypt.hash(this.password, salt) 不起作用?

[英]why hashedPassword = await bcrypt.hash(this.password, salt) is not working?

我基本上只是嘗試使用 hash 使用 bcrypt 使用 async/await 設置密碼,但沒有任何效果...... next() 不工作,它沒有將數據保存到數據庫中,甚至沒有對密碼進行哈希處理

  const bcrypt = require("bcryptjs")
    userSchema.pre('save', async function (next) {
      try {
        const salt = await bcrypt.genSalt(10)
        console.log(this.email, this.password);
    
        const hashedPassword = await bcrypt.hash(this.password, salt)
    //above line making problem to me... I don't know but below the above line code is not working... plz help me to figure out the mistake
    
        this.password = hashedPassword
        console.log(`the hashed password is ${this.password}`);
    
        next()
    
      } catch (error) {
        next(error)
      }
    })
     

嘗試這個:

const bcrypt = require('bcrypt')

let letBcrypt = async function() {

let salt = await bcrypt.genSalt(10)
console.log('salt:',salt)
const hashedPassword = await bcrypt.hash('ali', salt)
if(!hashedPassword ){
// something went wrong
  console.log('something went wrong')
} else {
 // successful
  console.log('hsashedPass:',hashedPassword)
}
  
}

letBcrypt();
  

結果:

salt: $2b$10$9btuRjCf/ddGsHG9qCIABu
hsashedPass: $2b$10$9btuRjCf/ddGsHG9qCIABuS616GHRHDekz8Ub1tKVrgQu.OjEYnWe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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