0

I am trying to make a request to the database (mongoDB) and save its return in a list of objects but the list is not getting filled. Here is the code

router.get('/all/:studentEmail', auth, async (req, res) => {
    try {
        const student = await Student.findOne({ email: req.params.studentEmail });
        if (!student) {
            return res.status(404).json({ msg: 'Student not found' });
        }
        var list = [];

        student.exercises.map(async (exercise) => {
            list.unshift(await Exercise.findById(exercise));
        });

        res.json(list);
    } catch (err) {
        console.error(err.message);
        res.status(500).send('Server error');
    }
});

The database query await Exercise.findById(exercise) returns correctly the object, but res.json(list); returns empty. Do anyone know how to solve it?

1
  • Not sure if this is the issue but you aren't passing an object to res.json -- list is an array, not an object. Maybe try res.json({ list }). Commented Oct 22, 2020 at 21:26

1 Answer 1

1

The base issue is that res.json() executes way before student.exercises.map(async (exercise) => { completes. Putting await into map doesn't wait for each and every item in the async loop to process. Either use something like Promise.all() or use a for loop (other strategies can be used also). Decide which to use based on whether you can process in parallel or need to process in series. Try the following using Promise.all to execute async requests parallel using then on each Promise to execute an operation against list:

router.get("/all/:studentEmail", auth, async (req, res) => {
  try {
    const student = await Student.findOne({ email: req.params.studentEmail });
    if (!student) {
      return res.status(404).json({ msg: "Student not found" });
    }
    var list = [];

    await Promise.all(
      student.exercises.map((exercise) =>
        Exercise.findById(exercise).then((result) => list.unshift(result))
      )
    );

    res.json(list);
  } catch (err) {
    console.error(err.message);
    res.status(500).send("Server error");
  }
});

Also, an alternative to unshift and just return the results if they are not nested, if they are nested you can consider flat():

const list = await Promise.all(
  student.exercises.map((exercise) => Exercise.findById(exercise))
);

return res.json(list);

Hopefully that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

@BrennoLima Glad to hear it worked. Mark the answer if it helped resolve your issue. Thanks!

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.