简体   繁体   English

MongoDB 存储“承诺” object 而不是散列密码

[英]MongoDB stores “Promise” object instead of hashed password

I am using bcrypt to hash passwords and MongoDB as my database.我使用 bcrypt 到 hash 密码和 MongoDB 作为我的数据库。

Here is the code:这是代码:

export default function buildMakeUser({pwdHasher})
{
    return function makeUser({
        username,
        email,
        password,
        password_hash = pwdHasher(password), // the important part
        favoriteColor
    } = {})
    {

// ...
        return Object.freeze({
            getUsername: () => username,
            getEmail: () => email,
            getHashedPassword: () => password_hash,
            getFavoriteColor: () => favoriteColor
        });
}

And here is pwdHasher 's definition:这是pwdHasher的定义:

import bcrypt from "bcrypt";
import buildMakeUser from "./entity/user.js";

async function pwdHasher(password){
    let hashed;
    hashed = await bcrypt.hash(password, 10);
    return hashed;
}

However, when I store the user in the database, here's the result:但是,当我将用户存储在数据库中时,结果如下:

  ops: [
    {
      username: 'kibe',
      email: 'blabla@gmail.com',
      password_hash: [Promise],
      _id: 5ecc8b752e0aa53e87d5b62a
    }
  ],

It seems like makeUser 's object does not wait for pwdHasher(password) .似乎makeUser的 object 不等待pwdHasher(password) I have tried wrapping pwdHasher in a Promise and it also did not work.我曾尝试将pwdHasher包装在 Promise 中,但它也没有用。

Does anyone know why?有谁知道为什么?

Thank you.谢谢你。

There are two solutions I see based on your code snippet:根据您的代码片段,我看到了两种解决方案:

First way: (Recommended)第一种方式:(推荐)
Use await when calling the async function.调用async function 时使用await The code will be:代码将是:

export default function buildMakeUser({pwdHasher})
{
    return function makeUser({
        ...
        password_hash = await pwdHasher(password), // the important part 
        ...
    })  
}

Alternative way:替代方式:
Use hashSync method of bcrypt instead of using async-await.使用bcrypthashSync方法而不是使用 async-await。

You have defined pwdHasher as:您已将pwdHasher定义为:

async function pwdHasher(password) { ... }

But you call it as:但你称它为:

password_hash = pwdHasher(password),

By definition, an async function RETURNS A PROMISE.根据定义,异步 function 返回 PROMISE。 If you'd like to get the value from the promise, you must either await the result or use pwdHasher(password).then(...)如果您想从 promise 获取值,您必须await结果或使用pwdHasher(password).then(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点 mongodb 将数据存储为字符串而不是 object - Node mongodb stores data as string instead of an object 在使用Node.js服务器将哈希密码发布到数据库之前,如何在承诺中传递哈希密码? - How to pass a hashed password in a promise before posting it to the database using a Node.js server? 当我在客户端 hash 我的密码时,我没有得到散列字符串,而是得到密码本身 - when I hash my password on the client side , I am not getting the hashed string instead getting the password itself 散列密码对我不起作用 - hashed password not works with me localstorage只存储一个对象,而不是添加连接它们 - localstorage only stores one object instead of adding concatenating them 角度js-返回promise代替数据对象 - angular js - Return promise instead data object AngularJS服务返回一个对象而不是一个承诺 - AngularJS Service to Return an Object Instead of a Promise Firestore 返回 True 或 False 而不是 Promise Object - Firestore Return True or False instead of the Promise Object 承诺取回承诺 <void | T> 而不是预期的对象 - Promise fetch returning Promise<void | T> instead of expected object 返回 [object Promise] 而不是实际值 - Returning [object Promise] instead of actual value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM