I have this code to hash strings and put them in an object list. The hashed strings say ‘undefined’ in the terminal when I print the object list. When I print the hash from the function, it clearly shows the hash.
What am I missing here?
const bcrypt = require('bcrypt')
const users = []
function hashPassword(password, salt) {
bcrypt
.hash(password, salt)
.then(hash => {
console.log(hash)
return hash
})
.catch(err => console.log(err.message))
}
const hashedPassword = hashPassword('asdasdasd', 10)
users.push({
"ID": 01,
"Name": "Ali Haider",
"Email": "[email protected]",
"Password": hashedPassword
})
users.push({
"ID": 02,
"Name": "Kumai",
"Email": "[email protected]",
"Password": hashedPassword
})
console.log(users)
Here’s the result when I run the code:
PS D:_Programmingjs> node app.js
[
{
ID: 1,
Name: 'Ali Haider',
Email: '[email protected]',
Password: undefined
},
{
ID: 2,
Name: 'Kumai',
Email: '[email protected]',
Password: undefined
}
]
JSON file created.
$2b$10$qEap/oHdL4hrRN9T6Ahr2eZyoIu96Q.23qiHFa0eD1DeN65OhZQB2