【问题标题】:typescript function throwing error to return a value打字稿函数抛出错误以返回值
【发布时间】:2017-11-25 06:09:15
【问题描述】:

这是我想要完成的,我正在调用一个函数 getGeoLocationOfUser(),我应该返回用户的地理位置,并且函数应该仅在地理位置可用或出现错误时返回。

但上述函数抛出错误声明类型既不是“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;
});
}

我可能在这里遗漏了一个非常基本的概念。任何帮助将不胜感激。

【问题讨论】:

  • 很公平。@Rob 我最终放了很多 cmets,因为有很多问题的答案..人们急于成为 FGITW

标签: angular typescript ionic2


【解决方案1】:

您需要在此处返回地理位置的Promise

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

【讨论】:

  • 正是我想要的。谢谢
【解决方案2】:

请更改返回类型any 而不是{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;
        });
} 

【讨论】:

  • 这不行..虽然箭头函数返回值,但箭头函数本身并不是从getGeoLocationOfUser返回的
  • 值得注意的是,使用 any 应该是使用 TypeScript 时的最后手段。在这种情况下,这是完全没有必要的。
【解决方案3】:

您可以尝试使用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;
});
}

【讨论】:

  • 这行不通..虽然箭头函数返回值,但箭头函数本身不是从getGeoLocationOfUser返回的,它应该返回一个promise..
  • 检查我的答案.. return this.geolocation.getCurrentPosition()
猜你喜欢
  • 1970-01-01
  • 2017-03-18
  • 2015-06-12
  • 2019-10-12
  • 2020-08-27
  • 1970-01-01
  • 2022-01-12
  • 2018-10-05
  • 1970-01-01
相关资源
最近更新 更多