title: How to get rid of the asynchoronous code here
body: <p>I have been trying to retrieve the data using the MongoJS driver for node.js.The Code which I am using is as follows</p>
  
  <pre><code>     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('&lt;html&gt;&lt;body&gt;');
               db.frames.find({str_id:story}).toArray(function(err,doc){
               console.log(doc);
               for(var t=0;t&lt;doc.length;t++)
                   {
                       var picid=doc[t].pic_id;
                       console.log(picid);               
                       db.pictures.find({_id:picid}).toArray(function(err,pic){
                        res.write('&lt;img src="'+pic[0].name+'"/&gt;');
               });
             } 
          }) 
          res.end('&lt;/body&gt;&lt;/html&gt;'); 
     });
  </code></pre>
  
  <p>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.</p>