0

I am not getting the value of arraymedicationArray at the end of the for loop.I want to see the value of medicationArray in the if (medicationArray != null & medicationArray.length > 0) {. Could someone help me with it? I am not sure what I am missing here.

var firstPageURL = medUrl + "?startDate=" + moment(startDate).format(dateFormat) + "&endDate=" + moment(endDate).add(1, 'day').format(dateFormat) + "&pageNumber=1";
    var medRequest = $.getJSON(firstPageURL, function (data) {  
        pageData = data;    
        var jsonData = pageData.Medications;
        medicationArray.push(pageData.Medications);     
        }
    }).done(function (data) {
    
        if (pageData.Number >=1) {
            var requestsProcessed = 0;
        
            for (var i = 1; i <= pageData.Number; i++) {
            var pageURL = medicationUrl + "?startDate=" + moment(startDate).format(dateFormat) + "&endDate=" + moment(endDate).add(1, 'day').format(dateFormat) + "&pageNumber=" + i;
                var pageRequest = $.getJSON(pageURL, function (data) {
                    requestsProcessed++;
                    medArray = data.Medications;                    
                    for (var j = 1; j <= pageData.Number; j++) {
                        medicationArray.push(medArray[j]);
                    }
                    
                    if (requestsProcessed === (pageData.Number - 1)) {
                        toggleDateRangeSearch(true);
                        $("#iconSpin").css('display', 'none');
                    }
                })
                    .fail(function () {
                        errorPage.push(i);
                        $("#iconSpin").css('display', 'none');
                        toggleDateRangeSearch(true);
                    });
    
                setTimeout(function () {
                    if (!requestSuccess) {
                        $("#pMessage").css('display', 'block');
                        pageRequest.abort();
                    }
    
                }, 20000);
            }
            
            if (medicationArray != null & medicationArray.length > 0) {
                buildMedicationTable(medicationTable, medicationArray);         
            }
    
        }
        
    })
        
Aarav
  • 3
  • 5
  • I am not sure how this question duplicates? I checked the above answer and that does not give my answer. – Aarav Feb 26 '21 at 05:38

1 Answers1

0

use && not &

if (medicationArray != null && medicationArray.length > 0)

also, I guess you want to use it in function, not outside...

  var pageRequest = $.getJSON(pageURL, function(data) {
      requestsProcessed++;
      medArray = data.Medications;
      for (var j = 1; j <= pageData.Number; j++) {
        medicationArray.push(medArray[j]);
      }

      if (requestsProcessed === (pageData.Number - 1)) {
        toggleDateRangeSearch(true);
        $("#iconSpin").css('display', 'none');
      };

      // use the array here, not before
      if (medicationArray != null && medicationArray.length > 0) {
        buildMedicationTable(medicationTable, medicationArray);
      }
    })

ok, found another way... you are passing a parameter in url, pageNumber we can get this in "always" event if it is last "always" event, you can add your code here...

var medicationUrl = 'https://jsfiddle.net/test.html?user=HainKurt';
var pageData = {
  Number: 3
};

for (i = 1; i <= pageData.Number; i++) {
  var pageURL = medicationUrl + "?startDate=" + "20210102" + "&endDate=" + "20210131" + "&pageNumber=" + i;
  var pageRequest = $.getJSON(pageURL, function(data) {
      ...
    })
    .fail(function() {
      ...
    })
    .always(function() {
      var pn = urlParam(this.url, "pageNumber");
      if (pn == pageData.Number) {
        console.log("ALL DONE! Run your code here, array is ready!");
      }
    });
}

function urlParam(url, name) {
  return (url.split(name + '=')[1] || '').split('&')[0];
}

https://jsfiddle.net/HainKurt/kvhr3tcq/

after all, your code should be like this

var firstPageURL = medUrl + "?startDate=" + moment(startDate).format(dateFormat) + "&endDate=" + moment(endDate).add(1, 'day').format(dateFormat) + "&pageNumber=1";
var medRequest = $.getJSON(firstPageURL, function(data) {
  pageData = data;
  var jsonData = pageData.Medications;
  medicationArray.push(pageData.Medications);
}
}).done(function(data) {

  if (pageData.Number >= 1) {
    var requestsProcessed = 0;

    for (var i = 1; i <= pageData.Number; i++) {
      var pageURL = medicationUrl + "?startDate=" + moment(startDate).format(dateFormat) + "&endDate=" + moment(endDate).add(1, 'day').format(dateFormat) + "&pageNumber=" + i;
      var pageRequest = $.getJSON(pageURL, function(data) {
          requestsProcessed++;
          medArray = data.Medications;

          for (var j = 1; j <= pageData.Number; j++) {
            medicationArray.push(medArray[j]);
          }

          if (requestsProcessed === (pageData.Number - 1)) {
            toggleDateRangeSearch(true);
            $("#iconSpin").css('display', 'none');
          }
        })

        .always(function() {
          var pn = urlParam(this.url, "pageNumber");
          if (pn == pageData.Number) {
            console.log("ALL DONE! Run your code here, array is ready!");
            if (medicationArray != null & medicationArray.length > 0) {
              buildMedicationTable(medicationTable, medicationArray);
            }
          }
        })

        .fail(function() {
          errorPage.push(i);
          $("#iconSpin").css('display', 'none');
          toggleDateRangeSearch(true);
        });

      setTimeout(function() {
        if (!requestSuccess) {
          $("#pMessage").css('display', 'block');
          pageRequest.abort();
        }

      }, 20000);
    }
  }
})

function urlParam(url, name) {
  return (url.split(name + '=')[1] || '').split('&')[0];
}
HainKurt
  • 134
  • 6
  • Hi @HainKurt, I don't think that is an issue. I am not even getting value in `medicationArray`. Please show me a way to get value in it. If you notice, I am using `for (var j = 1; j <= pageData.Number; j++) { medicationArray.push(medArray[j]); }` and storing value in that. – Aarav Feb 26 '21 at 05:35
  • why you are starting with 1, should be 0 I guess... `for (var j = 0; j < pageData.Number; j++) { medicationArray.push(medArray[j] );` also, before this, when you use `console.log(medArray);`, what do you get? – HainKurt Feb 26 '21 at 05:40
  • I am getting array in that. I don't think that is an issue. – Aarav Feb 26 '21 at 05:43
  • then use `console.log(pageData.Number)` what do you get here? – HainKurt Feb 26 '21 at 05:46
  • I am getting value in that as well but this is for pagenumber. – Aarav Feb 26 '21 at 05:52
  • if you have a number in pageData.Number (>=1), then after the loop, if you use `console.log(medicationArray)` you should see the data... – HainKurt Feb 26 '21 at 05:56
  • Yes I can see the data. As I mentioned, I am using `if (medicationArray != null && medicationArray.length > 0)` outside of `for` loop. So `medicationArray` is carrying any data that is the problem why I am not able to see any value. – Aarav Feb 26 '21 at 05:59
  • see my answer... that code is running before ajax call, so you will not get anything... use it when ajax call returns something... – HainKurt Feb 26 '21 at 06:07
  • I can't use it inside the function. There is a reason behind it. If I use it inside the function then `buildMedicationTable(medicationTable, medicationArray);` will create a table after each page. To avoid this, I am trying to store it in `medicationArray` and build `buildMedicationTable` only once. So I am using it outside of function so that it will be called only once after it completes the loop. – Aarav Feb 26 '21 at 06:13
  • Where to use ` buildMedicationTable(medicationTable, medicationArray);` in the code. How your code will build the table if We are not going to use this then. – Aarav Feb 26 '21 at 09:30
  • Please provide me the proper solution. I am looking forward to it. I have already added my code to the question. – Aarav Feb 26 '21 at 10:32
  • I am new to SO... add this function to your code, `urlParam`. then add `.always(function() {...}` to your ajax code and inside this function, use your code `if (medicationArray != null & medicationArray.length > 0) { buildMedicationTable(medicationTable, medicationArray); }` – HainKurt Feb 26 '21 at 16:48