【问题标题】:Promise and subscribe承诺并订阅
【发布时间】:2017-09-21 15:11:51
【问题描述】:

我有一个 Angular2 (ionic2) 应用程序。我有一个请求城市的函数,但我收到一个错误,即 this.cityService.getAllCities() 上不存在属性订阅。

cityPage.ts 有这样的功能:

getCities(){
    this.cityService.getAllCities()
          .subscribe(cityData => { this.cityList = cityData; },
            err => console.log(err),
            () => console.log('Complete!')
    );
}

我的 cityService.getAllCities() 函数如下所示:

getAllCities(){

    return new Promise (resolve => {
        this.storage.ready().then(() => {

            this.storage.get('authData').then(authData => {
              let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
                authData.access_token });
              let opt = new RequestOptions({ headers: hdr });
                return this.http.get(AppSettings.API_GET_CITIES).map(res => <CityModel[]> res.json(), opt);
            }).catch(() => {
              //resolve(false);
            });

        });

    });

  }

编辑

根据评论,我已经像这样更改了我的功能:

getAllCities(){

    return Observable.create(resolve => {
        this.storage.ready().then(() => {

            this.storage.get('authData').then(authData => {
              let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
                authData.access_token });

                console.log('access_token ' + authData.access_token);
              let opt = new RequestOptions({ headers: hdr });
                 return this.http.get(AppSettings.API_GET_CITIES,opt).map(res => <CityModel[]> res.json()).subscribe((result) => {
                  console.log(result);
                  resolve = result;
                });
            }).catch(() => {
              //resolve(false);
            });

        });

    });

  }

在我的console.log(result) 中,我收到了数据,但数据从未返回到我的getCities() 函数。也没有调用console.log('Complete!')

【问题讨论】:

  • Promise.Then... 订阅观察者

标签: angular ionic2 angular2-services


【解决方案1】:

它抛出错误的原因,因为 .subscribe 方法确实可以在 Observable 上监听,只要它发出数据。这里从getAllCities 方法返回一个promise,你可以在它上面应用.then 函数来获取从Promise 返回的数据

getCities() {
  this.cityService.getAllCities()
    .then(
       cityData => { this.cityList = cityData; },
       err => console.log(err),
       () => console.log('Complete!')
  );
}

并且还通过在http.get() Observable 上调用.toPromise() 方法从getAllCities 方法返回promise。

getAllCities(){

    return new Promise (resolve => {
        this.storage.ready().then(() => {

            this.storage.get('authData').then(authData => {
              let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
                authData.access_token });
              let opt = new RequestOptions({ headers: hdr });
              //returned promise from here.
                return this.http.get(AppSettings.API_GET_CITIES)
                   .map(res => <CityModel[]> res.json(), opt)
                   .toPromise();
            }).catch(() => {
              //resolve(false);
            });
        });
    });
}

【讨论】:

  • 上面给了我解决方案,尽管我不得不更改一些代码。而不是return this.http.get 我不得不做resolve(this.http.get)
  • @kwv84 return this.http.get(AppSettings.API_GET_CITIES) .map(res =&gt; &lt;CityModel[]&gt; res.json(), opt) .toPromise() 也应该可以解决问题
  • @PankajParkar 对我来说同样的事情,必须添加 resolve(this.http.get)...我还保留 .toPromise()
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 2017-07-29
  • 2017-12-12
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
相关资源
最近更新 更多