0

I have a function where I work through an array to go get distances for our locations. Upon return from that call I want to update the original Array with this new distance value. It is working in all browsers except IE (testing on IE11). It appears that the array is considered undefined.

Error: Unable to set property 'LocationDistance' of undefined or null reference

LocationsArr does exist, and I can pull/see values from it in the outer function, but not within the success: function of the $.ajax call.

Any help would be hugely appreciated!

function getLocationDistance() {
  let origin = UserLatitude + "," + UserLongitude
  let l = 0
  let distance = 0
  for (let i = 0; i < LocationsArr.length; i++) {
    let data = {
      ak: "XXXXXXXXXXXX",
      origin: origin,
      ID: LocationsArr[i].LocationID,
      destination: LocationsArr[i].LocationDestination
    };
    $.ajax({
      type: 'GET',
      url: "https://www.kaleidahealth.org/GoogleAPIServices/DistanceMatrix.svc/GetTripInfo",
      data: data,
      cache: false,
      dataType: "jsonp",
      success: function(result) {
        distance = result.distance.replace(" miles", "")

        // IE CAN'T SEE LocationsArr ARRAY - ALL OTHER BROWSERS CAN
        LocationsArr[i].LocationDistance = distance;

        l++;
        if (l === i) {
          buildLocationList();
        }
      },
      error: function(err) {
        alert("ERROR: " + JSON.stringify(err))
      }
    })
  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I think the problem is with `i`, not `locationsArr`. It sounds like IE11 doesn't properly implement `let` bindings in `for` loops. – Barmar Sep 02 '20 at 17:03
  • I agree. It has to do with i. Ironically, I converted all my instances let to var and now it doesn't work in any browser. – Brian DeCicco Sep 02 '20 at 19:54
  • I switched the for loop to $.each(LocationsArr, function(i) {} and all is well now. Thanks for the suggestion! – Brian DeCicco Sep 02 '20 at 20:20

0 Answers0