0

I have an angular question.

I am using googles geocode (which is async).

I have a variable 'this.list' that is populated when I enter my function. Then I call geocoder.geocode(...) which is an async function.

However, my already defined variable is undefined inside.

here is a snippet:

 alert('entering map init: the list has ' + this.list?.length??0);  //verified it is populated
    var geocoder = new google.maps.Geocoder();
    var coords = new google.maps.LatLng(0, 0);


     geocoder.geocode({ 'address': centeringAddress }, function (results, status)
    {    alert(this.list);   //it now is undefined

... nonrelevent code...}

When I enter the function... I see my 'this.list' has 6 items. I call geocoder.geocode, and then this.list is undefined.

I understand since it is async things set within would not be set outside since it is async; however, why is an already defined variable undefined within the geocode call?

Any suggestions on how to get my 'this.list' item populated within the geocode call?

Thanks Chris

CodeMusic
  • 87
  • 10

1 Answers1

0

User MikeOne resolved this issue in a comment under the main post.

He suggested changing 'function (results, status)' to '(results, status) =>' in my originally posted code.

This fixed the issue. I would love to know the difference between the two syntaxes; however, regardless I am very happy this worked.

Thank-you MikeOne, and jengelbells for the super fast replies!!

CodeMusic
  • 87
  • 10