Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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 promise.then()在promise.resolve()之前执行_Node.js_Mongodb_Promise_Bluebird_Asynccallback - Fatal编程技术网

Node.js promise.then()在promise.resolve()之前执行

Node.js promise.then()在promise.resolve()之前执行,node.js,mongodb,promise,bluebird,asynccallback,Node.js,Mongodb,Promise,Bluebird,Asynccallback,我正在使用bluebird promise库并试图从MongoDb获取数据,问题是。然后promise函数在我从db获取数据之前执行。这是我获取数据的代码 function getCollection( collectionName ) { var p = database.listCollections().toArray( function(err, collInfos) { if ( err ) return null; for (var i = 0; i <= co

我正在使用bluebird promise库并试图从MongoDb获取数据,问题是。然后promise函数在我从db获取数据之前执行。这是我获取数据的代码

function getCollection( collectionName ) {

var p = database.listCollections().toArray( function(err, collInfos) {

  if ( err ) return null;

  for (var i =  0; i <= collInfos.length; i++) {
    console.log('getdata');
    if ( collInfos[i].name === collectionName ) {
      return collInfos[i];
    } 

    if ( i === collInfos.length - 1 ) {
      return null;
    }
  }

});

p = Promise.resolve( p ).bind( p ).then( function collection( res ) {
  console.log(res,'res');
  return res;
});

return p; }
函数getCollection(collectionName){ var p=database.listCollections().toArray(函数(err,fos){ if(err)返回null;
对于(var i=0;iMongoDB方法,只要您不传递回调,就返回承诺。因为您确实传递了一个回调,
p
变成了
未定义的
承诺。resolve
不知道等待什么。相反,使用

function getCollection( collectionName ) {
  return Promise.resolve(database.listCollections().toArray())
  .then(function(collInfos) {
    for (var i =  0; i <= collInfos.length; i++) {
      console.log('getdata');
      if ( collInfos[i].name === collectionName ) {
        return collInfos[i];
      }
    }
    return null;
  }, function(err) {
    return null;
  })
  .then( function collection( res ) {
    console.log(res,'res');
    return res;
  });
}
函数getCollection(collectionName){ 返回Promise.resolve(database.listCollections().toArray()) .然后(功能(FOS){
对于(var i=0;i这意味着
toArray
不返回承诺,
p
未定义且
promise.resolve
不知道等待什么。@Bergi那么如何从
toArray
返回承诺,并返回实际响应,即null或collection对象?如果没有传递回调,则返回承诺。顺便问一下,有什么问题<代码>绑定(p)
应该怎么做?