Node.js Mongodb collection.countDocuments 回调不起作用



我正在尝试编写一个Node.js应用程序,该应用程序使用MongoDB Node.js驱动程序3.4获取特定MongoDB集合中最近添加的项目。 据我了解,这样做的方法是使用 collection.countDocuments 将文档的数量传递到回调中,创建一个跳过该数字的游标 - 1,然后调用一个函数来实际返回文档。 我的问题是对collection.countDocuments的回调似乎从未真正运行过。

我的代码(包括控制台.log调用调试:

mongo.connect(dburl, (err, client) => {
console.log("Attempting to connect to database");
if(err) {
console.log("Couldn't connect to server");
throw err;
}
else {
var heatdata;
try {
var db = client.db(dbname);
console.log("Database: " + db.databaseName);
var dbo = db.collection(colName);
console.log("Collection: " + dbo.collectionName);
dbo.countDocuments({}, function(err,count) {
console.log("Count");
if (err) throw err;
dbo.find({}).skip(count - 1).toArray((err, docs) => {
console.log("Made it to skip");
if (err) {
res.writeHead(500, {'Content-Type': 'text/html'});
res.write('<h1>Error on accessing database information.</h1>');
console.log("Couldn't get the requested document");
res.end();
throw err;
}
else {
console.log("Result: " + docs);
heatdata = docs;
console.log("In method: " + heatdata);
}
});
console.log("Past the find");
});
console.log("Past the count");
}
catch(e) {
console.log("Error caught: " + e.name);
throw e.message;
}
console.log("Out of method: " + heatdata);
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(heatdata);
res.end();
}
});

预期的控制台输出:

Attempting to connect to database
Database: myDatabase
Collection: myCollection
Count
Made it to skip
Result: {foo:bar}
In method: {foo:bar}
Past the find
Past the count
Out of method: {foo:bar}

实际输出:

Attempting to connect to database
Database: myDatabase
Collection: myCollection
Past the count
Out of method: undefined
TypeError: First argument must be string or Buffer

换句话说,来自dbo.countDocuments的代码根本没有运行,包括第一个日志语句,表明它甚至首先输入了回调。 这里可能发生了什么?

提前谢谢。

编辑:回溯如下:

TypeError: First argument must be a string or Buffer
at write_ (_http_outgoing.js:642:11)
at ServerResponse.write (_http_outgoing.js:617:10)
at mongo.connect (C:Netshareheatgraph.js:72:21)
at result (C:Netsharenode_modulesmongodblibutils.js:414:17)
at executeCallback (C:Netsharenode_modulesmongodblibutils.js:406:9)
at err (C:Netsharenode_modulesmongodbliboperationsmongo_client_ops.js:286:5)
at connectCallback (C:Netsharenode_modulesmongodbliboperationsmongo_client_ops.js:241:5)
at process.nextTick (C:Netsharenode_modulesmongodbliboperationsmongo_client_ops.js:463:7)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)

请注意,当我注释掉countDocuments块并将其替换为dbo.find().limit(1).sort({$natural:-1}).next(callback);时,我得到完全相同的回溯。

编辑 2:因此,当我运行在 http 侦听器之外截取的原始代码时,它提供了以下控制台输出:

Attempting to connect to database
Connection: true
Database: faketemps
Collection: temps
Past the count
Out of array method: undefined
Connection: true
Count
Past the find
Made it to skip
Result: [object Object]
In array method: [object Object]

所以基本上,回调似乎没有运行的原因是我没有给它足够的时间来实际运行。 我将尝试将其全部包装在一个setInterval中,以尝试几次实际获取某些东西,然后在适当的时间点超时。 无论如何,正式学到的关于异步编程的教训。

countDocuments 函数是这样用的:

dbo.countDocuments(requestObj, options, callback)

第二个参数是选项参数(有关更多详细信息,请参阅 https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/(。

希望这会有所帮助。

最新更新