繁体   English   中英

node.js和express:顺序执行流程一个mongodb查询请求一个接一个

[英]node.js and express : Sequential execution flow one mongodb query request after another

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

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

        });
    });
};

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

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

使用回调或承诺或流控制库。 您必须至少不了解这些方法中的一种,才能在节点中对服务器进行编程,并且老实说,所有中庸的节点编程人员都充分理解了这三种方法(包括少数几个不同的流控制库)。

这不是什么,您只会在stackoverflow上得到其他人为您编码的答案,然后继续前进。 这是您必须去学习和学习的基本事物,因为它只会每天反复出现。

http://howtonode.org/control-flow

http://callbackhell.com/

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM