搜索

首页  >  问答  >  正文

node.js - mongodb多条查询,如何在所有查询执行完成后再回显到页面。

router.get('/test.node', function(req, res, next) {
    //定义回显的msg
    var msg = {
        stats: 1,
        err: [],
        main: {},
        mark: ''
    };
    //查询数据1
    var paywayList = [];
    //查询数据2
    var tradeList = [];
    //查询数据3
    var rechargeList = [];
    //查询数据4
    var tradeSum = {};
    
    //每查询一次调用一次okCountFunc()
    //查询数据1
    bossService.getPaywayListByMerchant(merchantId, branchId, function(items) {
        paywayList = items;
        okCountFunc();
    });
    //查询数据2
    bossService.getPaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(items) {
        tradeList = items;
        okCountFunc();
    });

    //查询数据3
    bossService.getVippaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(items, objs) {
        rechargeList = objs;
        okCountFunc();
    });
    //查询数据4
    bossService.getDaysumListByMerchant(merchantId, branchId, params.startDate, params.endDate, function(list, sum) {
        tradeSum = sum;
        okCountFunc();
    });
    
    //统计计数
    var okCount = 0;
    var okCountFunc = function() {
        okCount++;
        if (okCount == 4) { //到这里查询已全部结束
            //最后合并该统计数据
            msg.main.paywayList = paywayList;
            msg.main.tradeList = tradeList;
            msg.main.rechargeList = rechargeList;
            msg.main.tradeSum = tradeSum;
            res.send(msg);
        }
    }
});
迷茫迷茫2789 天前656

全部回复(1)我来回复

  • phpcn_u1582

    phpcn_u15822017-05-02 09:28:15

    雷雷

    回复
    0
  • 取消回复