0

I have below query with mongodb. I am getting array of exercises from the front end and I need to insert them one by one. And also in every exercise I am getting sets so I need to loop again through them and need to create multiple exercise on the behalf of sets as well

Here is the code in my nodejs file

const createExercises = payload.exercises.map(async(exercise) => {
  for (let i = 1 ; i <= exercise.sets ; i++) {
    console.log(i)
    return (await Exercise.findOneAndUpdate(
      { user, exerciseName: exercise.exerciseName, workoutName: payload.workoutName, sets: i },
      { $set: {
        exerciseName: exercise.exerciseName, sets: i 
      }},
      { upsert: true }
    ))
  }
})
console.log(createExercises)
await Promise.all(createExercises)

return reply({ success: true, message: 'success' })

But the problem is I am not able to wait for the response. What I am missing here.

Edit --> Now the problem is I am getting this in console for console.log(i) and console.log(createExercises)

1
[ Promise {<pending>, domain: Domain { } } ]
2
3
4
5

What should I see

1
2
3
4
5

[ Promise {<pending>, domain: Domain { } } ]

Thanks!!!

9
  • You return from the first iteration of your loop?! Commented Dec 3, 2018 at 7:44
  • @Bergi So from where do I return it? Commented Dec 3, 2018 at 7:44
  • What do you want to return from the map callback - or rather, do you want to return anything at all? Commented Dec 3, 2018 at 7:46
  • I don't want to return anything. Just want that all the exercise should be created in database and then I should go further in reply Commented Dec 3, 2018 at 7:48
  • Then drop the return and it should work, sequentially awaiting every findOneOrUpdate in your loop, and running all the loops concurrently for all your exercises Commented Dec 3, 2018 at 7:51

1 Answer 1

1

For loops not waiting with promises to be resolved. you should use map. I see you have map which is correct but inside map you are using standard for loop which is not correct in case of Promise waiting, see my example with nested map

const createExercises = payload.exercises.map(async(exercise) => {
  return await Promise.all(exercise.sets.map(async (set, index) => {
    return (await Exercise.findOneAndUpdate(
      { user, exerciseName: exercise.exerciseName, workoutName: payload.workoutName, sets: i },
      { $set: {
        exerciseName: exercise.exerciseName, sets: index
      }},
      { upsert: true }
    ))
  }))
})
console.log(createExercises)
await Promise.all(createExercises)

return reply({ success: true, message: 'success' })
Sign up to request clarification or add additional context in comments.

2 Comments

I have added a fix with awit to Promise.all inside. Let me know if that works for you, the best will be to have fiddle to test that with real usecase.
Thanks +1 for your help

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.