0

我有一个在 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
            });

        });
    });
};
4

3 回答 3

9

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

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);

对于循环仿真示例:

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);
于 2013-10-04T07:33:00.080 回答
0

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

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

http://howtonode.org/control-flow

http://callbackhell.com/

于 2013-10-04T07:12:11.943 回答
0

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

于 2014-02-11T20:33:27.860 回答