Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
calling map on a collection after it has been returned vs chaining map using the dot

This works as expected:

    let responses = await Response.find({
      responder_id: req.user._id,
    }).populate({ path: "query_id", select: ["query_type", "content"] });

    responses = responses.map((response) => {
      return response.query_id;
    });

But the below does not:

    let responses = await Response.find({
      responder_id: req.user._id,
    })
      .populate({ path: "query_id", select: ["query_type", "content"] })
      .map((response) => {
        return response.query_id;
      });

The only thing that I can think of is maybe it has something to do with the asynchronous nature of these functions.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Priority is not as you think it is. What is being awaited is not Response.find(...), but Response.find(...).populate(...). With this in mind, here's the way to write it in the second form correctly (with the only difference being a pair of parentheses):

let responses = (await Response.find({
  responder_id: req.user._id,
})
  .populate({ path: "query_id", select: ["query_type", "content"] }))
  .map((response) => {
    return response.query_id;
  });

However, some re-indenting would be needed to make this readable. The first snippet might be easier to read anyway.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!