0

I am trying to bcrypt password for every user in an array.

router.post("/insertuser", (req, res) => {

  var promises = users.map((item) => {

    bcrypt.genSalt(10)
      .then((salt) => {
        return item
      })    
  })

  Promise.all(promises)
    .then((results) => {
      console.log(results)
      res.json({
        "data": results
      })
    })    
})//end route

But I am getting results = [undefined,undefined].

How can I return array element from bcrypt.genSalt(10).then

Please help as I am new to ES6

EDIT: My user users array is like this:

[{ "username": "admin", "admin": true} ]

6
  • 1
    your map callback doesn't return anything, therefore, promises will be an array of undefined - you need to return something in (item) => { /* here */} Commented Nov 3, 2017 at 5:32
  • How can I correct that. I am new to this. Please help. Commented Nov 3, 2017 at 5:33
  • 2
    ... perhaps return bcrypt.genSalt(10) - that way you're returning something Commented Nov 3, 2017 at 5:33
  • But i want to update user.password field inside the .map Commented Nov 3, 2017 at 5:36
  • Or just don’t use a block. Just use ….map(item => bcrypt.genSalt(10).then(salt => item)). Commented Nov 3, 2017 at 5:37

2 Answers 2

1

Simply return the promise from bcrypt.genSalt.

router.post("/insertuser", (req, res) => {

  var promises = users.map((item) => {

    return bcrypt.genSalt(10)
      .then((salt) => {
        return item
      })    
  })

  Promise.all(promises)
    .then((results) => {
      console.log(results)
      res.json({
        "data": results
      })
    })    
})//end route
Sign up to request clarification or add additional context in comments.

2 Comments

Simply return the bcrypt.genSalt's result. - it's actually going to return the Promise, not the result, and the resolved value will be item
Thanks a lot sir. You saved my day.
0

When you add .then() after any promise it will directly get resolved. In your code users.map() will run synchronously and the promises will have undefined. Here is the code you can use :

router.post("/insertuser", (req, res) => {
    var promises = users.map((item) => {
      return bcrypt.genSalt(10);
    })

    Promise.all(promises)
      .then((results) => {
        console.log(results)
    });  
})//

Also notice that salt is used to generate hash. You are only generating salt. To generate hash of password also add bcrypt.hash(password,salt). Here is the code :

var promises = users.map((item) => {
  return bcrypt.genSalt(10);
})

Promise.all(promises)
  .then((results) => {
    promises = results.map((item, index) => {
      return bcrypt.hash(users[index], item);
    });
    return Promise.all(promises);
  })
  .then(result => {
    console.log(result);
  })
  .catch(err => {
    console.log(err);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.