0

I can't get the result of a resolved promise inside the export default section.

Hi devs, I'm new to JavaScript and I'm having trouble with promises and export default section. I'm hashing a password but I can't get the value inside my users' array object. I can get the password on my console though, and I want to use that array in my controllers to populate it with other users using postman.

My userModel module that I'm importing in my controllers.

import hasher from '../helpers/password'; 

const adminPassword = async () =>{
    const hashedPwd = await hasher.hashingPassword('john124', 10);
    console.log(hashedPwd); 
    return hashedPwd;   
};


 export default [
     {
        id: 1, 
        first_name: 'john', 
        last_name: 'doe', 
        email: 'john@gmail.com', 
        password: adminPassword(),
        address: 'kigali', 
        is_admin: true  
    }
]

Response body from Postman

{
    "status": 200,
    "message": "Successfully Signed Up",
    "data": [
        {
            "id": 1,
            "first_name": "john",
            "last_name": "doe",
            "email": "john@gmail.com",
            "password": {},
            "address": "kigali",
            "is_admin": true
        },
        {
            "id": 2,
            "email": "james@gmail.com",
            "first_name": "james",
            "last_name": "mes",
            "password": "$2a$10$XXCc4oMwawyWZMzJJdUyq.Z.l9YobO3jicg6x7qNN/v7.94c9qVg.",
            "address": "kinshasa",
            "is_admin": false
        }
    ]
}

Console output

[ { id: 1,
    first_name: 'john',
    last_name: 'doe',
    email: 'john@gmail.com',
    password:
     Promise {
       '$2a$10$a575M8tm1b8QdkH./V0zSuxGUV43OapBzehXyH9CkpypqAr0hmsPK' },
    address: 'kigali',
    is_admin: true } ]

How can I get just the hashed string as my password? Thank you in advance.

Mo1
  • 369
  • 7
  • 18

1 Answers1

0

exports are handled synchronously. It's not possible to do this immediately. You could have an export eventually turn into a hashed password (and possibly have it return null first), but this would be a pretty bad approach.

The sane way to do this is to not return an object, but to return a promise that resolves into your full object:

const result = adminPassword.then( hash => {
  return {
    id: 1, 
    first_name: 'john', 
    last_name: 'doe', 
    email: 'john@gmail.com', 
    password: hash,
    address: 'kigali',
    is_admin: true  
  };
});
export default result;
Evert
  • 93,428
  • 18
  • 118
  • 189
  • thanks for the reply but two issues emerged by implementing your suggested solution: 1. when I use ```adminPassword.then``` without using parentheses on *adminPassword* I get the error that **then** is not a function. 2. Even though I'm able to get the hashed password as the value. I'm unable to insert the object inside the users' array. Because the **push method** is not working. I'm getting this error: ```TypeError: _userModel.default.push is not a function```. – Mo1 May 20 '19 at 06:36
  • @Mo1 it looks like your usernmodel was wrapped in array, I didn't do this. Don't copy my example verbatim though, try to figure out what I did and apply it to your case. The general idea is that you want to probably return a promise with the full value. – Evert May 20 '19 at 14:29
  • Thank you so much. I was able to push the object inside the array using ```Promise.all``` because my array contained promises. For anyone who would face the same issue, here is the link: https://stackoverflow.com/questions/51113973/javascript-promise-push-value-into-array-only-from-function-or-outside – Mo1 May 23 '19 at 10:13