On Jan 30, 2015, at 4:33 AM, Atul K wrote:
> 
> I am working on node.js http server. The Server is connected to mongodb. I am 
> requesting a post request to the server to get documents from mongodb. But 
> the post response is not waiting for mongodb callback to complete. And 
> therefore I am not getting required output on the client side. How to handle 
> this?
> 
> http.createServer(function(request, response) {
>     if(request.method == "POST") {
>         var body = '';
>         request.on('data', function(chunk) {
>             console.log(chunk.toString());
>             body += chunk;
>         });
>         request.on('end', function() {
>             MongoClient.connect("mongodb://localhost:27017/exampleDb", 
> function(err, db) {
>                 if(err) {
>                     console.log("We are not connected");
>                 }
>                 else {
>                     var sysInfo = db.collection('sysInfo');
>                     var jsonObj = sysInfo.find().toArray();
>                     response.writeHead(200, {'Content-Type': 'text/plain'});
>                     response.end(jsonObj);
>                 }
>             });
>         })
>     }
> });

I'm not sure which node mongodb module you're using, but undoubtedly an 
operation like find() will be asynchronous--a request will be sent to the 
mongodb server, and it will take some time to complete, and when it is 
completed, your callback will be called with the resuls. You will need to 
consult the documentation of whichever node mongodb module you're using to see 
how it expects you to provide the callback that it will call when the 
asynchronous operation is complete.

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/FE8B0FCE-7A44-4AA0-977A-396996B706BF%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to