在所有查询运行后发送响应

时间:2018-05-31 06:57:16

标签: node.js mongodb async.js

我有一个数组,我在数据数组中定义为“from”和“to”的时间。我想一直运行查询以从集合中获取计数,然后将所有数据存储在一个数组中,然后将其作为响应发送。我过去一直在使用异步方法,而且我对它很新。我通过谷歌搜索获得了异步代码。这是我控制器中的代码

exports.production_report = function(req, res) {
   ShiftTiming.findOne({"clientId":req.body.clientId}, function (err, 
shiftTiming) {
     if(err)
       res.send(err);
     else{
       var data = [];
       var productionCount = [];
       for (var i = 0; i <= 23; i++) {
         var from = new Date(req.body.eDate);
         from.setHours(shiftTiming.shiftA[0]+shiftTiming.shiftA[1]);
         from.setMinutes(shiftTiming.shiftA[3]+shiftTiming.shiftA[4]);

         var to = new Date(from);
         to.setHours(to.getHours() + 1);
         if (i>0) {
           from.setHours(from.getHours() + i);
           to.setHours(to.getHours() + i);
         }
         data.push({"from":from,"to":to});
       }

       async.each(data, function (user, cb) {
         async.waterfall([
           function (cb) {
             console.log(data.from);
             CycleTime.count({
               "machineId":req.body.machineId,"startDateTime":{ $gte :data.from, $lt : data.to}
            }).exec(function(err, count) {
               cb(err, count);
               console.log(count);
            });
          },
          function (count, cb) {
            cb(err, count);
          },

         ], function (err, count) {
            if(err) return callback(err);
              productionCount.push({
                count
              });
            })
      })
      res.json({
            data: data,
            productionCount: productionCount
          });
  //console.log(data);
  //res.json(data);
    }
  });
};

我发送的请求是

{
    "clientId":"DEMO",
    "eDate":"2018-05-23",
    "machineId":"DEMO01"
}

我发送的回复是

{
"data": [
    {
        "from": "2018-05-23T02:30:00.000Z",
        "to": "2018-05-23T03:30:00.000Z"
    },
    {
        "from": "2018-05-23T03:30:00.000Z",
        "to": "2018-05-23T04:30:00.000Z"
    },
    {
        "from": "2018-05-23T04:30:00.000Z",
        "to": "2018-05-23T05:30:00.000Z"
    },
    {
        "from": "2018-05-23T05:30:00.000Z",
        "to": "2018-05-23T06:30:00.000Z"
    },
    {
        "from": "2018-05-23T06:30:00.000Z",
        "to": "2018-05-23T07:30:00.000Z"
    },
    {
        "from": "2018-05-23T07:30:00.000Z",
        "to": "2018-05-23T08:30:00.000Z"
    },
    {
        "from": "2018-05-23T08:30:00.000Z",
        "to": "2018-05-23T09:30:00.000Z"
    },
    {
        "from": "2018-05-23T09:30:00.000Z",
        "to": "2018-05-23T10:30:00.000Z"
    },
    {
        "from": "2018-05-23T10:30:00.000Z",
        "to": "2018-05-23T11:30:00.000Z"
    },
    {
        "from": "2018-05-23T11:30:00.000Z",
        "to": "2018-05-23T12:30:00.000Z"
    },
    {
        "from": "2018-05-23T12:30:00.000Z",
        "to": "2018-05-23T13:30:00.000Z"
    },
    {
        "from": "2018-05-23T13:30:00.000Z",
        "to": "2018-05-23T14:30:00.000Z"
    },
    {
        "from": "2018-05-23T14:30:00.000Z",
        "to": "2018-05-23T15:30:00.000Z"
    },
    {
        "from": "2018-05-23T15:30:00.000Z",
        "to": "2018-05-23T16:30:00.000Z"
    },
    {
        "from": "2018-05-23T16:30:00.000Z",
        "to": "2018-05-23T17:30:00.000Z"
    },
    {
        "from": "2018-05-23T17:30:00.000Z",
        "to": "2018-05-23T18:30:00.000Z"
    },
    {
        "from": "2018-05-23T18:30:00.000Z",
        "to": "2018-05-23T19:30:00.000Z"
    },
    {
        "from": "2018-05-23T19:30:00.000Z",
        "to": "2018-05-23T20:30:00.000Z"
    },
    {
        "from": "2018-05-23T20:30:00.000Z",
        "to": "2018-05-23T21:30:00.000Z"
    },
    {
        "from": "2018-05-23T21:30:00.000Z",
        "to": "2018-05-23T22:30:00.000Z"
    },
    {
        "from": "2018-05-23T22:30:00.000Z",
        "to": "2018-05-23T23:30:00.000Z"
    },
    {
        "from": "2018-05-23T23:30:00.000Z",
        "to": "2018-05-24T00:30:00.000Z"
    },
    {
        "from": "2018-05-24T00:30:00.000Z",
        "to": "2018-05-24T01:30:00.000Z"
    },
    {
        "from": "2018-05-24T01:30:00.000Z",
        "to": "2018-05-24T02:30:00.000Z"
    }
],
"productionCount": []
}

我只想要每小时计数并在计算所有内容时发送它。 提前谢谢。

0 个答案:

没有答案