I have an object array in Node.JS and I wish to response.write(); it to the screen, I can manage to get the first object out but the loop stops and only the first is outputted, can anybody point me in the right direction...
db.collection('todo', function(err, collection){
collection.find(function(err, cursor) {
cursor.each(function(err, doc) {
for(docs in doc){
if(docs == "_id"){
}else{
var test = docs + " : " + doc[docs];
}
}
data = data.toString("utf8").replace("{{TEST}}", test);
response.write(data);
response.end();
})
});
});
Move response.end()
out of the loop. That should do it.
You need to change response.end(data);
to response.write(data);
Note that this will asynchronously append to the stream going to the user.
If you plan to dump them all at once you can also try:
cursor.toArray(function (err, docs) {
/** run a for loop over each doc in docs **/
})