1

I am novice in Angular2. Can someone please help me in below problem. I want to call 'update_settings' API after getting response of Login as login response/information is use in second API. What happen is I can't able to call the second API. It simply returns from login function without any output. Login API works fine but second not. In Angular 1 we use chaining but here I tried many things but nothing works. Can someone please help me in this regard?

login(username, password): Observable<any> {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    return this._httpService
      .post(
        'login_URL',
        JSON.stringify( { username: username, pass: password }),
        {headers}
      )
      .map(res => {
        if (res) {
          this._httpService.post('update_settings_URL', JSON.stringify( { client_id: res.client_id }))
          .map(new_res => {
            return { token: res.access_token };
          })
        }
      });
  }

http.service.ts

post(uri: string, body: string, options?: RequestOptionsArgs): Observable<any> {
    return this._http.post(this.endPointFullUrl(uri), body, this.requestOptionsArgs(options))
      .map(response => {
        try {
          return response.json();
        } catch (err) {
          /*In case if response body is empty, 200 response code also invokes
            error handlers as empty 'response.json()' throws exception.
            This way an undefined is returned as the response object
            which can easily be handled in the success observers down the chain.
          */
        }
      });
  }
AT82
  • 71,416
  • 24
  • 140
  • 167
Hassan Kashif
  • 435
  • 1
  • 7
  • 26
  • 1
    You are missing one `return`, but instead of that, maybe consider `flatMap` instead: https://stackoverflow.com/a/34107312/7741865 ? – AT82 Aug 05 '17 at 13:45
  • 2
    Possible duplicate of [Angular 2: Two backend service calls on success of first service](https://stackoverflow.com/questions/36712659/angular-2-two-backend-service-calls-on-success-of-first-service) – eko Aug 05 '17 at 13:47
  • Thanks to both of you. It worked! – Hassan Kashif Aug 07 '17 at 08:09

0 Answers0