0

Below i am using API to get country region from server using Observable and that country code is saved into sessionStorage():

let options = new RequestOptions({ headers: this.headers });
      return this.http.get(this.backendUrl + pathName, options)
                  .map((res: Response) => {
                        return res.json();
                    })
                  .catch(e => {
                      if (e.status === 401) {
                          this.router.navigateByUrl('/error');
                          return Observable.throw('Unauthorized');
                      }
                 });

and below is my another code where i want to use that country region code which is stored in sessionStorage() in post request:

let options = new RequestOptions({ headers: this.headers });
      body_val['hash_value'] = root.hashValue;
      body_val['region'] = <!-- Region -->;
      body_val['client_id'] = 'ABC';
      body_val['users_id'] = this.users_id;
      if (localStorage.getItem('userdata'))  {
          let user = JSON.parse(localStorage.getItem('userdata'));
          body_val['users_id'] = user.userdata.id;
          body_val['userid'] = user.userdata.id;
      }

      return this.http.post(root.apiUrl + pathName, body_val , options)
                   .map((res: Response) => {
                        return res.json();
                    })
                   .catch(e => {
                      if (e.code === 404) {
                           this.router.navigateByUrl('/error');
                           return Observable.throw('Unauthorized');
                      }
                   });

On first time when i load the page, i am getting null but in second time load the page i am able to get the value of sessionStorage() .How i can achieve first API call data is second API and on first page load.

Sahadev
  • 1,368
  • 4
  • 18
  • 39
  • they can't happen both at once and one after another. You need to make your send call wait for the first one to finish. – toskv May 18 '17 at 13:29
  • 1
    Possible duplicate of [Chaining RxJS Observables from http data in Angular2 with TypeScript](http://stackoverflow.com/questions/35268482/chaining-rxjs-observables-from-http-data-in-angular2-with-typescript) – toskv May 18 '17 at 13:30
  • above link does not works. can you suggest me how i can achieve this thing. – Sahadev May 19 '17 at 05:59

0 Answers0