4

I have called a function on ngOnInit() and now I am trying to call another function from the previous function but I am not able to get the results from second function

ngOnInit() {
    this.mapsAPILoader.load().then(() => {
        let autocomplete = new google.maps.places.Autocomplete(this.searchElement.nativeElement, {types: ['address']});
        autocomplete.addListener('place_changed', () => {
            this.ngZone.run(() => {
                let place: google.maps.places.PlaceResult = autocomplete.getPlace();
                if (place.geometry === undefined || place.geometry === null) {
                    return;
                }
                this.codeAddress(place.formatted_address, 'pickup');
            });
        });
    });
}

codeAddress(address: string, type) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, (results, status) => {
        if (status == google.maps.GeocoderStatus.OK) {
            this.getdistance();
        } else {
            alert('Request failed.');
        }
    });
}

getdistance() {
    console.log('get distance called');
}

Here in the code I am trying to call the getdistance() function from codeaddress() function. But it give me the error that this.getdistance() is not a function

1
  • Your code looks well . This is one the way to call the function . Commented Sep 10, 2019 at 5:31

4 Answers 4

2

You are trying to access this from within the callback you can use function(){}.bind(this) to make the function use the correct context, or use an ES6 arrow function.

geocoder.geocode({ 'address': address }, (results, status) => {
    if (status == google.maps.GeocoderStatus.OK) {
        this.getdistance();
    }
    else {
        alert("Request failed.");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Use an arrow function instead of function to use this pointer inside of it.

codeAddress(address: string, type) {
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': address}, (results, status) => {
        if (status == google.maps.GeocoderStatus.OK) {
            this.getdistance();
        } else {
            alert('Request failed.');
        }
    });
}

Comments

0

Answer pre-pending edit;

It's because you have a callback function with function(){..} instead of () => {..}. Thus your this is referring to the context of the inside of the function not the full class this scope.

Either change to an arrow function, or explicitly set this in the function:

var self = this
function(){
   this.getDistance()
}

Comments

0

context of this is changed when you have goes into the function , So do something like below otherwise use Arrow functions of ES6

codeAddress(address: string,type) {
     const self = this;
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode({ 'address': address }, function (results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
              self.getdistance();
           } else {
              alert("Request failed.");
           }
       });
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.