Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:32:46+00:00 2026-05-24T02:32:46+00:00

How do you do a join (i know it is not the correct term)

  • 0

How do you do a “join” (i know it is not the correct term) with an array of messages in mongoose?

I tried looping over all the messages and querying to get the user info but it is not working:

messages.forEach(function (message, index) {
  User.findById(message.userId, function (err, user) {
    messages[index].user = user
  })
})

console.log(messages) // the user info is not attatched

So how is this accomplished with mongoose and node.js?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:32:46+00:00Added an answer on May 24, 2026 at 2:32 am

    the biggest problem with your code is, that you assume the code to run synchronously – but it doesn’t. it runs asynchronously. so messages is not yet set when you execute

     console.log(messages);
    

    do something like this instead:

    var userIds = [id1, id2, id3];
    User.find({"_id": {$in: userIds}}, function (err, users) {
      console.log(users);
    });
    

    edit
    ok, i see. you want to add the userInfo to the different messages.
    easiest way to acieve this, is to use the async module: https://github.com/caolan/async

    async.map(messages, getUserInfo, function (err, result) {
      if (err) {
        console.log(err);
        return;
      }
      // log all msg with userinfo
      console.log(result);
    });
    
    function getUserInfo (msg, callback) {
      User.findById(msg.userId, function (err, user) {
        if (err) {
           callback(err);
           return;
        }
        msg.user = user;
        callback(null, msg);
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm filtering all user input to remove the following characters: http://www.w3.org/TR/unicode-xml/#Charlist (not suitable characters
This seems like something simple but I'm not able to get the syntax correct.
When I started writing database queries I didn't know the JOIN keyword yet and
I know you can combine multiple table-selects using a Join statement but is there
Anyone know how to do an update with a join (i.e. update on two
I'd like to know if having to conditionals when using a JOIN keyword is
Am interested to know how correct is my query the question is posted here
I have a question regarding left join on SQL: I would like to know
I have a stored procedure below, and I don't know if it's correct. I
I need to use the Spring Security User, Role/Authority and the UserRole-join classes both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.