0
我正在尝试迭代MongoDB游标对象,并从集合中返回所有记录,然后在节点outout中将响应方法用于浏览器。Node.JS HTTP响应返回什么?
我的问题是没有回应似乎被解雇。
我已经尝试把response.end()放入循环中,但它不会遍历结果。
我在不同的地方也尝试过response.end()。下面是代码
db.open(function(err, db) {
if(!err) {
console.log("Nodelistr is connected to MongoDB\n");
response.writeHead(200);
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);
//console.dir(doc);
})
response.end();
});
});
};
});
'data'未在初始化代码片断您发布 – soulcheck
更新的代码片段。 –
仍未初始化 – soulcheck