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);
                }
            });
        })
    }});

-- 
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/d6b3d788-7672-4557-be2a-f0ebed72c137%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to