【问题标题】:async.each nested in async.waterfallasync.each 嵌套在 async.waterfall 中
【发布时间】:2014-11-08 06:33:11
【问题描述】:

我最近开始使用异步 api。现在我的要求是对 3 个集合执行连接 即字段、脚本和语句。字段可以有多个脚本,脚本可以有多个语句。

这是我迄今为止尝试过的:(使用脚本加入 Fields 集合)

// Array to hold async tasks
var asyncTasks = [];

async.waterfall([
    function(callback){
        // fetches fields based on some Id and it returns 2 fields
        db.fields.find({entity_id: mongojs.ObjectId("54440a448bbbcbb4070131ab")}, function (err, fields) {
            console.log(JSON.stringify(fields, null, 2));
            callback(null, fields);
        })
    },
    function(arg1, callback){
        // arg1 now equals fields        
        arg1.forEach(function(eachField){
            asyncTasks.push(function(callback){
                db.scripts.find({fieldId: eachField._id.valueOf()}, function(err, scripts) {
                    // Async call is done then alert via callback
                    console.log(JSON.stringify(scripts, null, 2));
                    callback(null, scripts);
                });
            });
        });


        // Now we have an array of functions doing async tasks
        // Execute all async tasks in the asyncTasks array
        async.parallel(asyncTasks, function(err, results) {
            // All tasks are done now
            console.log("Scripts" + JSON.stringify(results, null, 2));
            callback(null, "done");
        });

    }
], function (err, result) {
        console.log(result);
});

// for the above code here is what i get the output
[
  {
    "_id": "54440a548bbbcbb4070131ac",
    "name": "t1",
    "type": "String",
    "entity_id": "54440a448bbbcbb4070131ab"
  },
  {
    "_id": "54447f1d20c103981fa1a27c",
    "name": "t2",
    "type": "String",
    "entity_id": "54440a448bbbcbb4070131ab"
  }
]
size of array 2
[]
[]
Scripts[
  [],
  []
]
done

即使数据库中有 2 个脚本,上述输出也不会打印任何脚本。我的数据库在 MongoDB 中,我使用的是 NodeJs、MongoJS api。为什么 db.scripts.find() 返回空数组? 任何帮助表示赞赏

我测试了这段代码,看看脚本是否返回了 o/p。请在下面找到我的代码

 test2();
    function test2(){

        var getScriptFunction = function(eachField, doneCallback){
            if(eachField !== undefined) {
                var fieldId = eachField;
                console.log(fieldId);
                db.scripts.find({fieldId: fieldId}, function (err, result) {
                    // Async call is done, alert via callback
                    doneCallback(null, result);
                });
            }
        }
        // The array is the id of fields
        async.map(["54440a548bbbcbb4070131ac", "54447f1d20c103981fa1a27c"], getScriptFunction, function (err, results) {
            // Square has been called on each of the numbers
            // so we're now done!
            if (err){
                console.log("error!" + err);
            } else {
                console.log("printed from helper function \n" + JSON.stringify(results, null, 2));
            }
        });
    }

这是上面代码的 o/p 来获取单独运行的脚本

printed from helper function
[
  [
    {
      "_id": "54440a678bbbcbb4070131ad",
      "name": "s1",
      "fieldId": "54440a548bbbcbb4070131ac"
    },
    {
      "_id": "544af260eb7a486824a5c306",
      "name": "s2",
      "fieldId": "54440a548bbbcbb4070131ac"
    }
  ],
  []
]

这就是字段的样子 (db.fields.find().pretty())

[
  {
    "_id": "54440a548bbbcbb4070131ac",
    "name": "t1",
    "type": "String",
    "entity_id": "54440a448bbbcbb4070131ab"
  },
  {
    "_id": "54447f1d20c103981fa1a27c",
    "name": "t2",
    "type": "String",
    "entity_id": "54440a448bbbcbb4070131ab"
  }
]

【问题讨论】:

  • 尝试将eachField._id 直接放在您的查找查询中,而不是eachField._id.valueOf()。只有给它一个真正的ObjectId 实例,查询才会正确匹配。
  • 它不起作用,因为 fieldId 保存为字符串值而不是 ObjectId。
  • 我尝试了你的建议,但仍然没有成功
  • 那么类型必须匹配才能使查询匹配文档。仔细检查 mongodb 中的数据和程序中查询的类型。

标签: node.js mongodb async.js


【解决方案1】:

我能够解决问题。有两个问题(1)我的回调函数名称相同,即内部和外部回调相互嵌套。 (2) 我必须使用 toString() 而不是 valueOf()

【讨论】:

    猜你喜欢
    • 2016-07-01
    • 2018-07-05
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 2012-08-27
    • 1970-01-01
    • 2014-06-28
    相关资源
    最近更新 更多