• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

190
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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error