来自mongodb的警告 – “(节点)警告:检测到recursion的process.nextTick。 这将在下一版本的节点中断开。 请使用setImmediate …“

那里,

我正在运行节点0.10.24,我试图从一个mongodb集合中获取所有logging,一旦我的集合超过1000个元素,我得到这个错误:

node.js:375抛出新的错误(msg); ^错误:(节点)警告:检测到recursionprocess.nextTick。 这将在下一个版本的节点中断。 请使用setImmediate进行recursion延期。 在CursorStream._next(/var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:76:)处的process.nextTick(node.js:480:9)处的maxTickWarn(node.js:375:15) 11)在CursorStream._onNextObject(/var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js:98:8)at /var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js: 78:12在Cursor.nextObject(/var/www/html/node_modules/mongodb/lib/mongodb/cursor.js:540:5)at /var/www/html/node_modules/mongodb/lib/mongodb/cursorstream.js :77:18在process._tickCallback(node.js:415:13)

我尝试了两种从mongodb中获取数据的方法,但没有成功。

版本1:

exports.findAll = function(req, res) { db.collection('products', function(err, collection) { var first = true; var stream = collection.find().batchSize(10000).stream(); var results = []; stream.on('data', function(item){ results.push(item); }).on('error', function (err) { // handle the error console.log(err); }).on('close', function () { // the stream is closed, so complete the response with res.end(); res.send(results); }); }); }; 

版本2:

 var sys = require('sys'), http = require('http'), mongodb = require('mongodb'); var server = new mongodb.Server("localhost", 27017, {}); var db = new mongodb.Db('productdb', server, {}); var client = null db.open(function (error, clientParam) { if (error) { db.close(); throw error; } client = clientParam; }); http.createServer(function(req, res) { var collection = new mongodb.Collection(client, 'products'); collection.find({}, {}, function(err, cursor) { if(err == null){ cursor.toArray(function(err, docs){ res.writeHead(200, {'Content-Type': 'application/json'}); res.write(docs); res.end(); //console.log(docs); }); }else{ res.writeHead(200, {'Content-Type': 'text/html'}); res.write('An error occurred!'); res.end(); throw err; } }); }).listen(8088); 

在版本3中,当我添加stream暂停和超时后,我恢复它,我会得到我想要的结果,但有一个重大的延迟。

版本3:

 exports.findAll = function(req, res) { db.collection('products', function(err, collection) { var first = true; var stream = collection.find().batchSize(10000).stream(); var results = []; stream.on('data', function(item){ results.push(item); stream.pause(); setTimeout(function(){ stream.resume(); }, 1); }).on('error', function (err) { // handle the error console.log(err); }).on('close', function () { // the stream is closed, so complete the response with res.end(); res.send(results); }); }); }; 

读取很多行的正确方法是什么,从mongodb使用node.js非常快,而不会遇到这个问题?

谢谢。

更新:

我发现另一种方式仍然不起作用,但是这次在第500行返回后,这个警告提示我:

 [WARNING] streamRecords method is deprecated, please use stream method which is much faster 

而这个代码是:

 db.collection('products', function(err, collection) { var stream = collection.find().streamRecords(); var results = []; stream.on('data', function(item){ results.push(item); }).on('error', function (err) { // handle the error console.log(err); }).on('end', function () { // the stream is closed, so complete the response with res.end(); res.send(results); }); }); 

如果我改变streamRecords()调用stream(),我回到我在第一版的第一个问题。如果我离开它,这样我只得到第一个500行。 :|

经过3天拉我的头发,我决定做一个干净的安装在我的服务器上,并安装所有最新的和最大的:

  "express": "3.x", "mongodb": "1.3.23", "connect-timeout": "0.0.1" "npm": "1.x" 

我以前用于node.js版本的mongodb驱动程序是:1.1.8,我认为这对我的悲伤负责。

我的版本1就像一个魅力!

只是想知道如果使用游标上的每个问题发生相同的问题。 所以只需稍微编辑一下你的代码(显然是改变了序列化位,但只是显示了放置):

 http.createServer(function(req, res) { var collection = new mongodb.Collection(client, 'products'); collection.find({}, {}, function(err, cursor) { if(err == null){ res.writeHead(200, {'Content-Type': 'application/json'}); cursor.each(function(err, doc){ if (doc != null) { res.write(doc); } }); res.end(); //console.log(docs); }else{ res.writeHead(200, {'Content-Type': 'text/html'}); res.write('An error occurred!'); res.end(); throw err; } }); }).listen(8088);