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

  • SEARCH
  • Home
  • 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 8770377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:33:00+00:00 2026-06-13T17:33:00+00:00

I have a list of items retrieved from mongoose each with a list object

  • 0

I have a list of items retrieved from mongoose each with a list object referenced, but I need to somehow populate the item.list.user object associated with each list so I can use them in my template as item.list.user.username.

Item.find().populate('list').exec(function(err, items){
  items.forEach(function(item){
    User.findById(item.list.user), function(err, user){
      item.list.user = user;
    });
  });

 //how to get back here from User.findById() so I can render?
 res.render('index', { items: items });
});
  • 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-06-13T17:33:01+00:00Added an answer on June 13, 2026 at 5:33 pm

    There are a few ways to go about this. The main issue is that you are assuming that the data will be populated when you render the template. That is not always the case, and you can and should always assume that any time you are doing asynchronous functions, that it won’t be done unless you wait until each function call is completed.

    Here is a naive way to make sure the data is available for render.

    Item.find().populate('list').exec(function (err, items) {
      var len = items.length
        , populatedItems = [];
      items.forEach(function(item, i){
        User.findById(item.list.user, function (err, user) {
          item.list = item.list.toObject();
          item.list.user = user;
          populatedItems.push(item);
          if (i + 1 === len) {
            res.render('index', { items: items });
          }
        });
      });
    });
    

    Though that is not very efficient and makes unnecessary database calls. It is also harder to reason about in my opinion.

    Item.find().populate('list').exec(function (err, items) {
      var itemMap = {}
      items.forEach(function (item, i) {
        // Map the position in the array to the user id
        if (!itemMap[item.list.user]) {
          itemMap[item.list.user] = [];
        }
        itemMap[item.list.user].push(i)
        item.list = item.list.toObject()
      });
      // Can pull an array of user ids from the itemMap object
      User.find({_id: {$in: Object.keys(itemMap)}}, function (err, users) {
        users.forEach(function (user) {
          itemMap[user._id].forEach(function(id) {
            // Assign the user object to the appropriate item
            items[id].list.user = user;
          })
        });
        res.render('index', { items: items });
      });
    });
    

    After further discussion with you on IRC and troubleshooting the following is a working example for your particular case.

    Item.find().populate('list').exec(function (err, items) {
      var itemIds = [];
      items.forEach(function (item) {
        itemIds.push(item.list.user)
      });
      // Can pull an array of user ids from the itemMap object
      User.find({_id: {$in: itemIds}}, function (err, users) {
        var userMap = {}
        users.forEach(function (user) {
          userMap[user._id] = user
        });
        res.render('index', { items: items, userMap: userMap });
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview which displays items retrieved from the webpage. Each item in
I have a List of Items which I retrieved from my Sqlite DB... I
I have a list which is dynamically built, but there are empty list items
I have a list of items, like a menu, where the current item has
I have a list of items on my web page that the user can
I have stored a list of items from gridView into the registry as below:
I populate web form with dynamic list of exams from database. I want user
I have a list of objects that I retrieved from a web service call
I have a tableView storing a list of notifications (retrieved from server). How can
I have a problem retrieving items from a fairly large list. I can quickly

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.