-
Notifications
You must be signed in to change notification settings - Fork 400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling services return "this is undefined" #321
Comments
You have to use arrow functions since the starter uses ES6 #280 (comment) try changing your functions to neither variables: this.onPositionUpdate = (position) => {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
console.log(lat, lng);
this.$log.log(lat, lng);
this.FoursquareService.getVenues(lat, lng).then(this.getVenuesData, this.unableToGetVenuesData);
}
this.getVenuesData = (data) => {
console.log(data.venues);
}
this.unableToGetVenuesData = (result) =>{
console.log(result);
} or functions outside the constructor as: constructor($log, FoursquareService) {
'ngInject';
this.$log = $log;
this.FoursquareService = FoursquareService;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(onPositionUpdate);
}
}
onPositionUpdate(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
console.log(lat, lng);
this.$log.log(lat, lng);
this.FoursquareService.getVenues(lat, lng).then(this.getVenuesData, this.unableToGetVenuesData);
}
getVenuesData(data){
console.log(data.venues);
}
unableToGetVenuesData(result){
console.log(result); =
} and instead of Also, i think you want to call a function when you call so your this.FoursquareService.getVenues(lat, lng).then(the_param_you_are_returning_in_your_service)=>{
this.getVenuesData(someParam);
this.unableToGetVenuesData(someParam)
}); |
Hi Flick36, thanks for the reply. I have use the ES6 by defining my functions outside of the constructor as you can see in my first code snippet. Unfortunately, this will cause an error where the My first thought is the code below will call onPositionUpdate function, and pass a
then, the function below will take the object and save it respectively. I try ro call service in this function, but they will return undefined error.
|
Do as i say: set the function as a variable in your constructor: like this:
|
Ok now I understand. I guess I have to start exploring es6 syntax. Thanks again flick36! |
Hi,
I have a problem calling my service in this starter kit. Below is my component
In the onPositionUpdate, the this method will get an error of undefined.
I have read through and the possible error is that the this method is nested.
I only get the API call if I do this, but still there is error:
Any ideas how to properly get the services working properly?
And here is my service if it is necessary:
The text was updated successfully, but these errors were encountered: