ERROR TypeError: this.Getaddress is not a function

Getaddress function was working fine for over a year but after doing npm update and updating some packages is not working.


 drawMap(): void {
    let map = Leaflet.map('map');
    Leaflet.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    }).addTo(map);

    map.locate({ setView: true });

    function onLocationError(e) {
      alert(e.message);
    }

    map.on('locationerror', onLocationError);
    map.on('dragend', function (MouseEvent) {

      this.Getaddress(map.getCenter()).then(data => {
        let content = data;
        console.log(content)
      });

    });
  }

    Getaddress(latlng) {
        let url = 'https://nominatim.openstreetmap.org/reverse?format=json&lat=' + this.LatLng.lat + '&lon=' + this.LatLng.lng + '&zoom=18&addressdetails=1';
        return new Promise(resolve =>
          this.http.get(url)
            .map(res => res.json())
            .subscribe(data => {
           resolve(data.results[1].formatted_address);
            }));
      }

There are a lot of problems here, but probably the most urgent and relevant is that you are losing execution context. Never type the word “function” inside the body of one. Use fat arrow functions instead.

1 Like

I am not professional coder but interesting it.What I learned is by try and error.

Anyway, I am so thankful for your guide. :rose::hibiscus: