簡體   English   中英

$ http.get請求以及其中的多個$ http.get請求無法正常運行

[英]$http.get request with multiple $http.get requests inside it not running properly

我嘗試了不同的方法來執行此操作,但是它們都失敗了。 問題是我有一個http.get請求,可以查詢給定日期(整天的總數)的數據,一旦返回,我就會在.then()內部運行多個http.get請求,以將這一天與用戶的班次相結合提供。 問題是說我有7天要每天查詢3個班次。 我運行查詢,並在一天結束后將其登錄到對象中。 這三個http請求同步運行,並應推遲到按鍵移位嵌套的那一天。 將會發生的事情是前幾項工作正常,但是將其推遲到第二天的奇數時間使第二天的總移位變為4,而不是3。其他時候,第一天和第二天的移位為2。 。 它永遠不會超過總班次,在這種情況下為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正在撥打電話:

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

某人可以給我打電話的任何幫助會等到其他人完成為止,因為那天似乎沒有等待輪班電話完成:|

我最終通過重新編寫要一個接一個地運行而不是彼此內部運行的函數來解決自己的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM