Promise and subscribe

╄→尐↘猪︶ㄣ 提交于 2019-12-19 02:40:28

问题


I have an Angular2 (ionic2) application. I have a function that request cities but I get an error that Property subscribe does not exists on this.cityService.getAllCities().

cityPage.ts has a function like this:

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

my cityService.getAllCities() function looks like this:

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

        });

    });

  }

Edit

Based on the comment I've changed my function like this:

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

        });

    });

  }

In my console.log(result) I receive data, but the data is never returned to my getCities() function. Also the console.log('Complete!') is not called.


回答1:


The reason it is throwing an error, because .subscribe method does available on Observable to listen to, whenever it emits a data. Here from getAllCities method you're returning a promise you could apply .then function over it to get data returned from that Promise

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

And also return promise from getAllCities method by calling .toPromise() method over http.get() Observable.

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


来源:https://stackoverflow.com/questions/43596530/promise-and-subscribe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!