将密码哈希为对象并收到Promise { }。

huangapple go评论67阅读模式
英文:

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(&#39;bcrypt&#39;)

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(&#39;pass&#39;)

users.push({
    &#39;ID&#39;: 0,
    &#39;Name&#39;: &#39;Ali&#39;,
    &#39;Email&#39;: &#39;alihaisul.me@gmail.com&#39;,
    &#39;Password&#39;: password
})

console.log(users)

OUTPUT:

[
  {
    ID: 0,
    Name: &#39;Ali&#39;,
    Email: &#39;alihaisul.me@gmail.com&#39;,
    Password: Promise { &lt;pending&gt; }
  }
]
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(&#39;bcrypt&#39;);
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(&#39;pass&#39;);

  users.push({
    &#39;ID&#39;: 0,
    &#39;Name&#39;: &#39;Ali&#39;,
    &#39;Email&#39;: &#39;alihaisul.me@gmail.com&#39;,
    &#39;Password&#39;: 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(&#39;pass&#39;).then(password =&gt; {
users.push({
    &#39;ID&#39;: 0,
    &#39;Name&#39;: &#39;Ali&#39;,
    &#39;Email&#39;: &#39;alihaisul.me@gmail.com&#39;,
    &#39;Password&#39;: password
})

console.log(users)

});




</details>



huangapple
  • 本文由 发表于 2023年5月7日 13:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定