简体   繁体   English

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

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

I'm basically trying to just hash a password using bcrypt using async/await but nothing is working... next() is not working and it is not saving the data into the database and even not hashing the password我基本上只是尝试使用 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)
      }
    })
     

try this:尝试这个:

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();
  

result:结果:

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