2017-06-22 18 views
3

ここで私が達成しようとしているのは、getGeoLocationOfUser()を呼び出しています。私はユーザーのジオロケーションを返すと仮定しています。何らかのエラーがあります。typescript関数が値を返すためにエラーをスローする

以上の関数はエラーをスローしています宣言された型が 'void'でも 'any'でもない関数は値を返す必要があります。

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

私はここで非常に基本的な考え方.ANYヘルプは理解されるであろうが欠落している可能性があります。

+3

十分なフェア。@ロブ私はissues..Peopleと答えの束があったので、多くのコメントを入れてしまった矢印関数が値を返すのに..ラッシュさFGITW –

答えて

3

の戻り値の型anyを変更します。

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

function().then(callback)を呼び出して値を取得できます。

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

正確に何を探していたのですか。お気軽に – Abhijeet

1

は親切にあなたがここにジオロケーションの約束を返却する必要がある代わりに{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; 
     }); 
} 
+2

にこの文句を言わない仕事です矢印関数自体は 'getGeoLocationOfUser'から返されません –

+0

OOPS!私は答えを更新しました。 –

+0

TypeScriptを使用する際には、 'any'を使用するのが最後の手段になるはずです。この場合は完全に不要です。 – Saravana

0

返信の種類は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

矢印関数が値を返すにもかかわらず、矢印関数自体が 'getGeoLocationOfUser'から返されません。 –

+0

は既に私の答えを編集しています@suraj – Devansh

+1

答え.. 'return this.geolocation.getCurrentPosition()' –

関連する問題