Reputation: 87
Here is what I am trying to accomplish, I am calling a function getGeoLocationOfUser() which I supposed to return me the geolocation of user and function is supposed to return only when geolocation is available or there is some error.
but above function is throwing an error A function whose declared type is neither 'void' nor 'any' must return a value.
public userGeolocation={latitude:null,longitude:null}
getGeoLocationOfUser():{latitude:any,longitude:any}{
this.geolocation.getCurrentPosition().then((resp) => {
this.userGeolocation.latitude=resp.coords.latitude;
this.userGeolocation.longitude=resp.coords.longitude;
console.log(this.userGeolocation);
localStorage.setItem('userGeoLocation',JSON.stringify(this.userGeolocation));
return this.userGeolocation;
//saving geolocation of user to localStorage
}).catch((error) => {
console.log('Error getting location', error);
return this.userGeolocation;
});
}
I might be missing a very basic concept here .Any help will be appreciated.
Upvotes: 4
Views: 1287
Reputation: 29614
You need to return a Promise of Geolocation here.
//Make return type as Promise<object_type> or Promise<any>
getGeoLocationOfUser():Promise<{latitude:any,longitude:any}>{
//return the inner function
return this.geolocation.getCurrentPosition().then((resp) => {
this.userGeolocation.latitude=resp.coords.latitude;
this.userGeolocation.longitude=resp.coords.longitude;
console.log(this.userGeolocation);
localStorage.setItem('userGeoLocation',JSON.stringify(this.userGeolocation));
return this.userGeolocation;
//saving geolocation of user to localStorage
}).catch((error) => {
console.log('Error getting location', error);
return this.userGeolocation;
});
}
You can then get the value by calling function().then(callback)
.
getGeoLocationOfUser().then( loc =>{
this.location = loc}).catch(err=>{});
Upvotes: 3
Reputation: 38683
Kindly change the return type any
instead of {latitude:any,longitude:any}
getGeoLocationOfUser(): any {
return this.geolocation.getCurrentPosition().then((resp) => {
this.userGeolocation.latitude = resp.coords.latitude;
this.userGeolocation.longitude = resp.coords.longitude;
console.log(this.userGeolocation);
localStorage.setItem('userGeoLocation', JSON.stringify(this.userGeolocation));
return this.userGeolocation;
//saving geolocation of user to localStorage
}).catch((error) => {
console.log('Error getting location', error);
return this.userGeolocation;
});
}
Upvotes: 1
Reputation: 1267
You can try with any
return type.
getGeoLocationOfUser(): Promise<any> {
this.geolocation.getCurrentPosition().then((resp) => {
this.userGeolocation.latitude=resp.coords.latitude;
this.userGeolocation.longitude=resp.coords.longitude;
console.log(this.userGeolocation);
localStorage.setItem('userGeoLocation',JSON.stringify(this.userGeolocation));
return this.userGeolocation;
//saving geolocation of user to localStorage
}).catch((error) => {
console.log('Error getting location', error);
return this.userGeolocation;
});
}
Upvotes: 0