1

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

  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)
      }
    })
     
  • Instead of bcryptjs, use bcrypt and perhaps try this. bcrypt.hash("trythisout1234", 10, (err, hash) => console.log(hash)); would be easier – BGPHiJACK May 05 '22 at 12:43
  • When I use bcrypt instead of bcryptjs and put this.password instead of "trythisout1234" ... Error: data must be a string or Buffer and salt must either be a salt string or a number of rounds... Above solution solve my half problem but still password is not hashing and data is storing now without password... Hashing password is coming as undefined – Priyanshu Gupta May 06 '22 at 02:30
  • when i use bcryptjs then it giving below error... Error: Illegal arguments: number, string at _async (C:\Users\PRIYANSHU\OneDrive\Desktop\MEN Projects\registration_form\node_modules\bcryptjs\dist\bcrypt.js:214:46) at Object.bcrypt.hash (C:\Users\PRIYANSHU\OneDrive\Desktop\MEN Projects\registration_form\node_modules\bcryptjs\dist\bcrypt.js:220:13) at model. (C:\Users\PRIYANSHU\OneDrive\Desktop\MEN Projects\registration_form\src\models\register.js:45:41 – Priyanshu Gupta May 06 '22 at 02:32
  • Don't mix up strings and integers, with original bcrypt the hash function takes (string, integer, callback). No need to await the process. – BGPHiJACK May 06 '22 at 15:19

1 Answers1

0

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
Ali Shefaee
  • 327
  • 3
  • 12
  • This answer is not working... const hashedPassword = await bcrypt.hash(this.password, salt) --> code is not working below this line. – Priyanshu Gupta May 06 '22 at 02:10
  • @PriyanshuGupta take a look to this online editor [example](https://replit.com/@Ali-AsgarAsgar1/bcrypt#index.js). (you can run it) I used [bcrypt](https://www.npmjs.com/package/bcrypt) package – Ali Shefaee May 06 '22 at 07:05
  • thanks @Ali Shefaee... now i got my mistake... await bcrypt.hash(this.password, salt)... here this.password === string ... i mistakenly store data as number instead of string – Priyanshu Gupta May 06 '22 at 13:10