Typescript error resetting function to return value

This is what I am trying to accomplish, I call the getGeoLocationOfUser () function, which I have to return to me that the geolocation of the user and the function should only be returned when geolocation is available or there is some error.

but the above function throws 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;
});
}

      

Perhaps I am missing a very simple concept. Any help would be appreciated.

+3


source to share


3 answers


You need to return the 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;
});
}

      



Then you can get the value by calling function().then(callback)

.

 getGeoLocationOfUser().then( loc =>{
     this.location = loc}).catch(err=>{});

      

+3


source


Remember to change the return type any

instead{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;
        });
} 

      

+1


source


You can try with return type any

.

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;
});
}

      

0


source







All Articles