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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:46:50+00:00 2026-06-13T02:46:50+00:00

I have been trying to retrieve the data using the MongoJS driver for node.js.The

  • 0

I have been trying to retrieve the data using the MongoJS driver for node.js.The Code which I am using is as follows

     req.on('end', function(){
             var decodedBody = querystring.parse(fullBody);
             story=decodedBody.name;
             var z=new Array();
             console.log(story);
         res.writeHead(200,{'Content-Type': 'text/html'});
         res.write('<html><body>');
             db.frames.find({str_id:story}).toArray(function(err,doc){
             console.log(doc);
             for(var t=0;t<doc.length;t++)
                 {
                     var picid=doc[t].pic_id;
                     console.log(picid);               
                     db.pictures.find({_id:picid}).toArray(function(err,pic){
                      res.write('<img src="'+pic[0].name+'"/>');
             });
           } 
        }) 
        res.end('</body></html>'); 
   });

The problem here is that because of the asynchronous nature of the code the response gets ends first and then the code inside the block of database gets executed and because of that nothing gets displayed on the browser i.e an image in this case .Thankx in advance.

  • 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-13T02:46:51+00:00Added an answer on June 13, 2026 at 2:46 am

    Don’t fight the asynchronous nature of node.js, embrace it!

    So you should fire off all your requests, marking each one as completed when the response arrives. When all requests are completed, render your images and body/html closing tags.

    I don’t usually work with node.js, so I can make some mistakes, but it may look like this:

    res.write('<html><body>');
    db.frames.find({str_id:story}).toArray(function(err,doc){
      console.log(doc);
    
      var completed = {};
    
      for(var t = 0; t < doc.length; t++) {
        var picid = doc[t].pic_id;
        completed.picid = false;
        console.log(picid);               
        db.pictures.find({_id: picid}).toArray(function(err, pic) {
          // mark request as completed
          completed.picid = pic;
    
    
          // check if all requests completed
          var all_finished = true;
          for(var k in completed) {
            if(completed[k] === false) {
              all_finished = false;
              break;
            }
          }
    
          // render final markup
          if(all_finished) {
            for(var k in completed) {
              var pic = completed[k];
              res.write('<img src="'+pic[0].name+'"/>');
            }
            res.end('</body></html>);
          }
        });
    
    
      } 
    }) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to retrieve data from Cassandra database using this command: select
I have been trying to create a ListView which I can sort using drag
Hi guys i have been trying to use cURL to send and retrieve data.
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I am trying to retrieve some data using a combination of jQuery and CakePHP.
I am trying to build a web service in .NET, which will retrieve data
I'm trying to understand a jquery script which stores an object using jquery data().
I have been trying to process a good chunk of data (a few GBs)
Have been trying to encrypt an xml file to a string so that I
Have have been trying to make a validator for my xml files. I have

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.