带有多个$ http.get请求的$ http.get请求无法正常运行

时间:2015-06-26 02:21:19

标签: javascript angularjs http http-get angularjs-http

我尝试过不同的方法,但都失败了。问题是我有一个http.get请求查询给定日期的数据(一整天的总数),一旦返回我在.then()内运行多个http.get请求,将这一天拼接到用户拥有的班次中提供。问题是说我有七天我想查询每天3班。我运行查询,当一天完成后,我将其记录到一个对象中。三个http请求同步运行,并且应该在关键转换下嵌套到那一天。会发生什么事情是前几个工作没有问题,但奇怪的是它推动了第二天的转变,使第二天的转变总数为4而不是3.其他时间它使第一天和第二天的转变有两个。它永远不会超过总变化应该是多少,在这种情况下21,但在整个日子里平衡。关于如何处理这个的任何想法。目前我正在做的事情看起来像这样

当天的主要功能:

function nextDay(startDate, endDate, processObj) {
    //console.log('I am in the next()');
    d = startDate;
    if (d <= endDate) {
      //console.log();

      da = new Date(d);
      da.setDate(da.getDate() + 1);
$http.get(WEB CALL HERE).then(function(response) {
        // Store the username, get the profile.
        dataAggregator(response.data, d, da);



      }).then(function() {
        if ($scope.reporting.shiftsEnabled === true) {

          var tempPromise = $q.defer();
          var tempCntr = 0;

          nextShift(tempPromise, tempCntr, startDate, endDate, processObj);


        } else {
          d.setDate(d.getDate() + 1);
          nextDay(d, endDate, processObj);
        }
      }, function(error) {
        console.log('Failure...', error);
      });

nextShift Call Are Made:

function nextShift(shiftPromise, cntr, startDate, endDate, processObj) {
    var d = startDate;
    if (cntr < $scope.shiftsObj.length) {


      var weekday = new Array(7);
      weekday[0] = "sunday";
      weekday[1] = "monday";
      weekday[2] = "tuesday";
      weekday[3] = "wednesday";
      weekday[4] = "thursday";
      weekday[5] = "friday";
      weekday[6] = "saturday";

      tempStartDate = new Date($scope.shiftsObj[cntr].startTime);
      tempStartDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempStartDate.getHours(), tempStartDate.getMinutes(), 0, 0);

      tempEndDate = new Date($scope.shiftsObj[cntr].endTime);

      if ($scope.shiftsObj[cntr].endTime < $scope.shiftsObj[cntr].startTime) {
        tempEndDate = new Date(da.getFullYear(), da.getMonth(), da.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
      } else {
        tempEndDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
      }
        var webShiftPromise = $q.defer();
        var webShiftCallReturn = webShiftCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
          processObj, tempStartDate, tempEndDate,
          $scope.reporting.hardware, webShiftPromise, cntr);

        webShiftCallReturn.then(function(retData) {


          var thePromise = $q.defer();
          shiftDataAggregator(retData, tempStartDate, tempEndDate, shiftPromise, cntr);

        }, function(error) {
          console.log('Failure...', error);
        });
      cntr++;
      nextShift(shiftPromise, cntr, startDate, endDate, processObj);
    } else {
      d.setDate(d.getDate() + 1);
      shiftPromise.resolve(nextDay(d, endDate, processObj));
    }
    return shiftPromise.promise;
  }

任何人都可以给我打电话的任何帮助等到其他人完成,因为似乎当天不等待轮班调用完成:|

1 个答案:

答案 0 :(得分:0)

我最后通过重新编写函数来一个接一个地运行而不是互相运行来修复我自己的问题......