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 9294457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:22:37+00:00 2026-06-18T21:22:37+00:00

I try get 3 documents with IDs: 1, 100, 1000. But node.js is async,

  • 0

I try get 3 documents with IDs: 1, 100, 1000. But node.js is async, and array objects in result is empty. How put documents into array?

var objects = {};
var objectsID = [1, 100, 1000];
objectsID.forEach( function( elem ){
  objectsDB.get( elem, function( err, object ){
    objects[elem] = object;
   })
 });
 console.log(objects);

console.log returns {}.

  • 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-18T21:22:39+00:00Added an answer on June 18, 2026 at 9:22 pm

    So this is a classical asynchronous code issue. Your code can be rewritten like this:

    var objects = {};
    var objectsID = [1, 100, 1000];
    
    var finalize = function( ) {
        console.log(objects);
    };
    
    objectsID.forEach( function( elem ){
        objectsDB.get( elem, function( err, object ){
            objects[elem] = object;
    
            // remove ID from the list of IDs
            var idx = objectsID.indexOf(elem);
            if (idx!=-1) {
                objectsID.splice(idx, 1);
            }
    
            // check if it is time to finalize
            if (!objectsID.length) {
                finalize();
            }
        })
    });
    

    You might want to have a look at Caolan’s async.js library as well ( in particular async.parallel ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need get documents from db by oid, like: Docs.objects(_id='4f4381f4e779897a2c000009') But how to do
i try to get my FragmentPagerAdapter working, but the examples are a bit to
I need to turn pages of PDF documents into separate PNG files, but I'm
I try to get all the div (some div are append by ajax) into
I try to get iPython and the notebook to work but I get this
I get this RSpec error when I try to embed one document in another.
I try get a my attribute inside Fancybox, and I get when I use
I try to get the whole content between an opening xml tag and it's
I try to get the main window handle using following code in Lazarus (Free
I try to get some website attribute (colour of the cell) and compare in

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.