【问题标题】:node.js and express : Sequential execution flow one mongodb query request after anothernode.js 和 express : 一个又一个 mongodb 查询请求的顺序执行流程
【发布时间】:2014-02-12 04:33:27
【问题描述】:

我有一个在 node.js 和 Express 中运行的网络服务器,它从 mongodb 检索数据。在 mongodb 中,集合是动态创建的,新创建的集合的名称将存储在一个元数据集合“项目”中。我的要求是首先迭代元数据集合以获取集合名称,然后进入每个集合内部根据某些条件进行多次查询。因为我的集合元数据是动态的,所以我尝试使用 for loop 来做。 但它给出了错误的数据。它没有执行顺序。在完成循环执行之前,它返回值。如何仅使用节点核心模块在 node.js 中执行顺序执行(不是其他库,如 async ..);

exports.projectCount = function (req, res) {
    var mongo = require("mongodb"),
        Server = mongo.Server,
        Db = mongo.Db;

    var server = new Server("localhost", 27017, {
        auto_reconnect: true
    });

    var db = new Db("test", server);
   // global JSON object to store manipulated data 
    var projectDetail = {
        projectCount: 0,
        projectPercent: 0
    };
    var totalProject = 0;
    db.open(function (err, collection) {
        //metadata collection 
        collection = db.collection("project");
        collection.find().toArray(function (err, result) {
            // Length of metadata  collection 
            projectDetail.projectCount = result.length;
            var count = 0;
            //iterate through each of the array which is the name of collection 
            result.forEach(function (item) {
                //change collection  object  to new collection
                collection = db.collection(item.keyParameter.wbsName);
                // Perform first query based on some condition 
                collection.find({
                    $where: "this.status == 'Created'"
                }).toArray(function (err, result) {
                    // based on result of query one increment the value of count 
                    count += result.lenght;
                    // Perform second query based on some condition 
                    collection.find({
                        $where: "this.status=='Completed'"
                    }).toArray(function (err, result) {
                        count += result.length;
                    });
                });
            });
            // it is returning the value without finishing the above manipulation
            // not waiting for above callback and value of count is coming zero .
            res.render('index', {
                projectDetail: projectDetail.projectCount, 
                count: count
            });

        });
    });
};

【问题讨论】:

    标签: javascript node.js mongodb express


    【解决方案1】:

    当你想调用多个异步函数按顺序,你应该调用第一个,在它的回调中调用下一个,依此类推。代码如下所示:

    asyncFunction1(args, function () {
      asyncFunction2(args, function () {
        asyncFunction3(args, function () {
          // ...
        })
      })
    });
    

    使用这种方法,您最终可能会得到一段难看且难以维护的代码。

    有多种方法可以在不嵌套回调的情况下实现相同的功能,例如使用async.jsnode-fibers


    这里是你如何使用 node.js EventEmitter:

    var events = require('events');
    var EventEmitter = events.EventEmitter;
    
    var flowController = new EventEmitter();
    
    flowController.on('start', function (start_args) {
      asyncFunction1(args, function () {
        flowController.emit('2', next_function_args);
      });
    });
    
    flowController.on('2', function (args_coming_from_1) {
      asyncFunction2(args, function () {
        flowController.emit('3', next_function_args);
      });
    });
    
    flowController.on('3', function (args_coming_from_2) {
      asyncFunction3(args, function () {
        // ...
      });
    });
    
    flowController.emit('start', start_args);
    

    for循环模拟示例:

    var events = require('events');
    var EventEmitter = events.EventEmitter;
    
    var flowController = new EventEmitter();
    
    var items = ['1', '2', '3'];
    
    flowController.on('doWork', function (i) {
      if (i >= items.length) {
        flowController.emit('finished');
        return;
      }
    
      asyncFunction(item[i], function () {
        flowController.emit('doWork', i + 1);
      });
    
    });
    
    flowController.on('finished', function () {
      console.log('finished');
    });
    
    flowController.emit('doWork', 0);
    

    【讨论】:

    • 但是如果代码是在for循环里面执行的,我就会知道所有的事件都执行完了然后返回。
    • @Sumeet 我添加了一个for循环模拟示例,这应该让您对实现其他迭代结构有所了解。
    • flowController.emit('doWork', i + 1) 没有调用具有增量值的 flowController.on('doWork') 。它也没有完成执行。输出只有 2。你能解释一下吗
    【解决方案2】:

    使用回调或承诺或流控制库。如果不了解这些方法中的至少一种,您就无法在 node 中对服务器进行编程,而且老实说,所有中等水平的 node 程序员都彻底了解这三种方法(包括少数不同的流控制库)。

    这不是你要在 stackoverflow 上得到其他人为你编码的答案然后继续前进的事情。这是您必须去学习和学习的基本知识,因为它只会每天一遍又一遍地出现。

    http://howtonode.org/control-flow

    http://callbackhell.com/

    【讨论】:

    【解决方案3】:

    根据我上面答案中的资源,在您迭代时嵌套回调并且仅在您处于最后一次迭代时调用它才能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 2012-11-06
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多