7

I'm working on my first node.js / express / mongoose app and I'm facing a problem due to asynchronisation mechanism of node.js. It seems I do not do the thing correctly...

Here is the test route I defined using express:

app.get('/test', function(req, res){
  var mod = mongoose.model('MyModel');
  mod.find({},function(err, records){
    records.forEach(function(record){
      console.log('Record found:' + record.id);
      // res.send('Thing retrieved:' + record.id);
    });
  });
});

When I issue a http://localhost/test, I'd like to get the list of records of type 'MyModel' in the response.

The code above is working fine but when it comes to returning this whole list to the client... it does not work (the commented res.send line) and only returned the first record.

I'm very new to node.js so I do not know if it's the good solution to embed several callback functions within the first callback function of app.get . How could I have the whole list returned ?

Any idea ?

1 Answer 1

11

What you should be doing is:

mod.find({},function(err, records){
  res.writeHead(200, {'Content-Length': body.length});
  records.forEach(function(record){
    res.write('Thing retrieved:' + record.id);
  });
});

Please always check the documentation as well:

http://nodejs.org/docs/v0.3.8/api/http.html#response.write

I missed that you was using express, the send function is part of express and extend's the serverResponse object of node (my bad).

but my answer still applies, express's send function sends the data using ServerResponse.end() so there for the socket get's closed and you cannot send data any more, using the write function uses the native function.

You may also want to call res.end() when the request is fully completed as some item's within express may be affected

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, got it. It's working fine like this (except the content-lenght that I removed). Thanks for the docs also.
whoop's that was supposed to be a content-type : text/plain, you may wan't to open a feature report with express and description that you cannot send chunked data via send, and to provide an alternative method of sending chunked text in further versions, what if you was doing a video stream ?
yes you'r right, thanks a lot for this advice. Also, regarding the res.end() function, would you use res.write() in the inner loop and res.end() at the end of the app.get callback function ?
the end method closes send's the any data supplied and then closes that connection socket, if you call the end function within a loop it will close the connection so you can't send any data, you should always call the end function but only when your 100% sure it's after the last chunk of data to be sent, so yes I would place that in the callback that's called after the loop has ended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.