我在试图成功查询Mongo的墙上敲打着我。
此代码:
async.waterfall([
function(callback){
cursor = db.collection(collection).findOne(query)
callback(null);
},
function(callback){
console.log("Result is:" + cursor);
console.log(JSON.stringify(cursor));
callback(null);
}
]);
产生以下输出:
result is:[object Object]
{}
为什么呢?它应该在集合中找到一个文档。
作为后续问题,我该如何看待
[object Object]
是
答案 0 :(得分:4)
基本上你应该等待你的查询完成,然后调用回调并期待任何结果:
async.waterfall([
function(callback){
db.collection(collection).findOne(query, function(err, result) {
callback(err, result); // if there is no err, it will be null
});
// the above can be simplified to just
// db.collection(collection).findOne(query, callback);
// since findOne callback and current function callback have the same arguments
},
function(result, callback) {
// use comma here to automatically doing JSON.stringiry(result)
console.log("Result is:", result);
callback();
}
], function(err) {
// here is your final callback where you know that async.waterfall
// is finished (with or without error)
});