英文:
Hashing password into an object and receiving Promise { <pending> }
问题
You are missing the fact that the hashPass
function is asynchronous, and it returns a promise. When you push the user object into the users
array, the password
field is still a pending promise because the hashing process is not complete yet.
To fix this issue, you should wait for the hashPass
function to complete before pushing the user object into the users
array. You can do this by using the await
keyword when calling hashPass
and wrapping it in an async
function like this:
(async () => {
const password = await hashPass('pass');
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
});
console.log(users);
})();
This way, the user object will only be pushed into the users
array once the password hashing is complete, and you won't see the "Promise { pending }" in the output.
英文:
I am learning to hash string passwords and store them in an object list.
const bcrypt = require('bcrypt')
var users = []
async function hashPass(pass)
{
try {
const salt = await bcrypt.genSalt(10)
console.log(`Hashing Password: ${pass}`)
const hashedPass = await bcrypt.hash(pass, salt)
console.log(`Hashed Password: ${hashedPass}`)
return hashedPass
} catch (error) {
console.log(error)
}
}
const password = hashPass('pass')
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
})
console.log(users)
OUTPUT:
[
{
ID: 0,
Name: 'Ali',
Email: 'alihaisul.me@gmail.com',
Password: Promise { <pending> }
}
]
Hashing Password: pass
Hashed Password: $2b$10$r52Lu1K.WxOEcAdyeMBV4eR3EPGRXsgJjktJMXFGY5F7a.Q6yoH1C
I keep getting the "Promise { pending }" in the output. I've researched and tried using async, await, and even the 'then()'. What am I missing here?
答案1
得分: 4
hashPass
函数使用 async/await 关键字,它返回一个 Promise 对象,你将其作为输出。
在将新用户对象推送到 users 数组之前,你需要 await hashPass
函数的结果。以下是如何修改你的代码来实现这一点:
const bcrypt = require('bcrypt');
var users = [];
async function hashPass(pass) {
try {
const salt = await bcrypt.genSalt(10);
console.log(`Hashing Password: ${pass}`);
const hashedPass = await bcrypt.hash(pass, salt);
console.log(`Hashed Password: ${hashedPass}`);
return hashedPass;
} catch (error) {
console.log(error);
}
}
async function createUser() {
const password = await hashPass('pass');
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
});
console.log(users);
}
createUser();
这样修改后,你的代码将等待密码哈希生成后再将用户对象添加到数组中。
英文:
As hashPass
function uses the async/await keywords it returns a Promise object and you are getting that as the output.
You need to await the result of the hashPass
function before pushing the new user object into the users array. Here's how you can modify your code to do that:
const bcrypt = require('bcrypt');
var users = [];
async function hashPass(pass) {
try {
const salt = await bcrypt.genSalt(10);
console.log(`Hashing Password: ${pass}`);
const hashedPass = await bcrypt.hash(pass, salt);
console.log(`Hashed Password: ${hashedPass}`);
return hashedPass;
} catch (error) {
console.log(error);
}
}
async function createUser() {
const password = await hashPass('pass');
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
});
console.log(users);
}
createUser();
答案2
得分: 1
你必须等待hashPassword
的承诺完成。请注意,async
函数最终也是承诺。
hashPass('pass').then(password => {
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
})
console.log(users)
});
英文:
You have to wait for the hashPassword
promise to finish. Note that async
functions are promises at the end.
hashPass('pass').then(password => {
users.push({
'ID': 0,
'Name': 'Ali',
'Email': 'alihaisul.me@gmail.com',
'Password': password
})
console.log(users)
});
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论