此函数返回null。我正在使用mongoDB和node和bluebird进行承诺

时间:2016-03-29 23:39:08

标签: nodes bluebird

这个函数应该返回一个具有给定id的文档:

function detailed(code,callback){
    MongoClient.connectAsync(murl).then(function(db) {
        return db.collection('reports').find({'_id':code});
    }).then(function(reports) {
        callback(null, reports)
    }).catch(function(err) {
        callback(err, null);
    });
}

2 个答案:

答案 0 :(得分:0)

我想这不是一个mongo相关的问题。 我想你刚刚错过了详细功能的退货声明。

function detailed(code,callback){

 return MongoClient.connectAsync(murl).then(function(db){

 return db.collection('reports').find({'_id':code});
 }).then(function(reports) {
    callback(null, reports)
 }).catch(function(err) {
     callback(err, null);
 });

}

答案 1 :(得分:0)

当您在蓝鸟中promisifyAll时,您需要将Async作为后缀添加到所有方法调用中。您正在调用不是蓝鸟方法的find - 使用.findAsync

此外,如果您需要使用回调,请不要使用.then(...).catch(...)使用asCallback(callback)

相关问题