You can try this.
Most importantly (maybe your trap?) Remember that "db.view" will mereley register a callback closure and continue. Do not close your request (by calling 'req.end') anywhere outside of this close. If you do this, most likely the request will be closed by db return. When the HTTP response object is closed, any data written to it loses its validity.
var cradle = require('cradle'); var db = new cradle.Connection().database('guestbook'); app.get('/guestbook', function(req, res) { // Register callback and continue.. db.view('guestbook/all', function(err, guests) { // console.log('The waiting had an end.. here are the results'); guests.forEach(function(guest) { if (guest.name) { res.write('Guest N: ' + guest.name); } }); // Close http response (after this no more output is possible). res.end('That is all!') }); console.log('Waiting for couch to return guests..'); // res.end('That is all!'); // DO NOT DO THIS!!! });
Torbjörn Österdahl
source share