Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js、mongodb和x2B;http服务器循环_Node.js_Mongodb_Scope - Fatal编程技术网

node.js、mongodb和x2B;http服务器循环

node.js、mongodb和x2B;http服务器循环,node.js,mongodb,scope,Node.js,Mongodb,Scope,我是node.js的新手(不到一个小时) 我试图打开一个简单的http服务器,它将读取mongodb集合并将数据打印到浏览器窗口 到目前为止,我已经: var http = require ("http") var mongodb = require('mongodb'); http.createServer(function(request, response) { var server = new mongodb.Server("127.0.0.1", 27107, {});

我是node.js的新手(不到一个小时)

我试图打开一个简单的http服务器,它将读取mongodb集合并将数据打印到浏览器窗口

到目前为止,我已经:

var http = require ("http")
var mongodb = require('mongodb');

http.createServer(function(request, response) {
    var server = new mongodb.Server("127.0.0.1", 27107, {});
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write('Collection Data:<br>')
    new mongodb.Db('testdb', server, {}).open(function (error, client) {
      if (error) throw error;
      var collection = new mongodb.Collection(client, 'test_coll');
      collection.find({}, {limit:100}).each(function(err, doc) {
        if (doc != null) {
            console.dir(doc.text);
            response.write(doc.text)
        }
      });
      response.write("some stuff")
      response.end();
    });
}).listen(8080)
var http=require(“http”)
var mongodb=require('mongodb');
createServer(函数(请求、响应){
var server=newmongodb.server(“127.0.0.1”,27107,{});
writeHead(200,{“内容类型”:“text/plain”});
response.write('收集数据:
') 新建mongodb.Db('testdb',server,{}).open(函数(错误,客户端){ 如果(错误)抛出错误; var collection=newmongodb.collection(客户机“test_coll”); collection.find({},{limit:100}).each(函数(err,doc){ 如果(doc!=null){ console.dir(doc.text); 响应。写入(文档文本) } }); 回答。写(“一些东西”) response.end(); }); }).听(8080)

这会将集合项的文本放在控制台上,但不会放在浏览器窗口中。我认为这是因为响应对象不在.each回调的作用域中?我的结构是否错误?

问题是在执行回调之前调用了
response.end()

您必须将其移到内部,如下所示:

collection.find({}, {limit:100}).each(function(err, doc){
    if (doc != null) {
        console.dir(doc.text);
        response.write(doc.text)
    } else {
        // null signifies end of iterator
        response.write("some stuff");
        response.end();
    }
});

res.end必须发生在回调内的for循环之后:

client.collection('test_coll', function(err, testColl) {
  testColl.find({}).each(function(err, doc) {
    if (err) {
        // handle errors
    } else if (doc) {
        res.write(doc._id + ' ')
    } else { // If there's no doc then it's the end of the loop
        res.end()
    }
  })
})

好的,我看到了,但这也不行-它将一个打印到客户机,然后结束响应。仍然没有骰子:TypeError:Object#没有方法“forEach”似乎仍然不这样做。在cursor.each()之后但在回调函数内部放置的任何内容仍然在cursor.each()输出之前执行。如果我把response.end()留在那里,我得到的只是“一些东西”。如果我完全去掉response.end(),我会得到“一些东西”,后面是我所期望的所有mongo数据。据推测,至少在本例中,我希望实际关闭请求,尽管在我的下一步列表中的某个地方是打开一个web套接字,该套接字将在数据可用时持续读取数据。有没有办法让光标读取块?@fields这是否回答了您的问题?