我想通过蓝鸟承诺在Node JS上的MongoDB中执行循环串行数据库插入,但无法这样做



>使用 for 循环我想通过猫鼬承诺插入数据并逐个处理输出

for (i = 0; i < array.length; i++) {
    if (xyz) {
        return Service.registerInterestsAsync(registerTruckArray)
            .then(function(x) {
                passoutputinArray(x)
            });
    }
}
我没有

使用过蓝鸟,但你可以通过编写自己的递归函数来做到这一点。

var handleResponse = function(err, data) {
    //perform action
}
function inserIntoMongo(dataArray, counter) {
  if( counter < dataArray.length) {
      mongoose.InsertDocument(dataArray[counter], function(err, data) {
          handleResponse(err, data);
          counter = counter + 1;
          insertIntoMongo(dataArray, counter);
      }
  }
}

最新更新