Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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
嵌套for循环MongoDB中的Javascript异步_Javascript_Node.js_For Loop_Async.js - Fatal编程技术网

嵌套for循环MongoDB中的Javascript异步

嵌套for循环MongoDB中的Javascript异步,javascript,node.js,for-loop,async.js,Javascript,Node.js,For Loop,Async.js,我在一个for循环中有一个异步函数,嵌套在另一个for循环中 // recipesArray is an array of arrays of objects // recipeObject is an array of objects // currentRecipe is an object connectToDb(function(){ // LOOP 1 for (var i=0, l=recipesArray.length; i < l; i++) {

我在一个for循环中有一个异步函数,嵌套在另一个for循环中

// recipesArray is an array of arrays of objects
// recipeObject is an array of objects
// currentRecipe is an object

connectToDb(function(){
   // LOOP 1
   for (var i=0, l=recipesArray.length; i < l; i++) {
      // recipeObject is an 
      var recipeObject = recipesArray[i];

      // LOOP 2
      for (var x=0, y=recipeObject.length; x < y; x++) {
         var currentRecipe = recipeObject[x];

         // this is an asynchronous function
         checkRecipe(currentRecipe, function (theRecipe) {
            if (theRecipe === undefined) {
               console.log('RECIPE NOT FOUND');
            } else {
               console.log('RECIPE FOUND', theRecipe);
            }
         });
      }
   }
});

//recipesArray是对象数组的数组
//recipeObject是一个对象数组
//currentRecipe是一个对象
connectToDb(函数(){
//回路1
对于(var i=0,l=recipesArray.length;i
我需要根据checkRecipe函数的结果向Recipes数组添加数据

我一直在尝试不同的事情。。。 -我有没有试着跟踪我和x。。。 -我是否尝试多次回调。。。 -我需要做所有这些吗,还是有其他的方法

我还尝试使用node的异步库(这实际上在其他情况下非常有用),但forEach不接受对象(只接受数组)

卡住了

如果您有任何建议,我们将不胜感激。

假设
checkRecipe()
可以无限制地并行运行,下面是如何使用
async。each()


你说,
forEach
只对数组有效,但你只有数组?或者至少
recipesArray
recipeObject
看起来都很像数组。嗯,这些对象是作为参数传递给回调函数的吗?也许给我们看看你试过的代码。如果我把这样的东西传递到async.forEach:[{color:red,size:med}]中,我将无法访问每个函数中的颜色和大小。@Bergi,我想这就是我遇到的问题:试着不要“编辑”原始对象,而是使用
map
而不是
each
。是的,
i
x
在您使用回调时会自动保存在闭包中。那么,
recipesArray
的一个小示例是什么样子的呢?[{color:red,size:med},{color:green,size:large}好的,那么您实际上没有数组数组了。你只有一个对象数组。我已经更新了我的答案来反映这一点。对不起,我说的是错误的(错误的数组)。recipesArray看起来像[[{key:value,key:value,key:value},{key-value….},{…}],{},{},{},{}]]一个对象数组的数组
connectToDb(function() {
  async.each(recipesArray, function(subArray, callback) {
    async.each(subArray, function(currentRecipe, callback2) {
      checkRecipe(currentRecipe, function(theRecipe) {
        if (theRecipe === undefined)
          return callback2(new Error('Recipe not found'));
        callback2();
      });
    }, callback);
  }, function(err) {
    if (err)
      return console.error('Error: ' + err);

    // success, all recipes found
  });
});