user1107888
user1107888

Reputation: 1525

Angular HttpClient Subscribe Issue

I am currently trying to return an Observable in Angular after an http Request. Following is the method code:

  public   fetchOptions(options ?: any): Observable<any> {

  let response: Subject<Response> = new Subject<Response>();
    if (!this._config) {
      return null;

    }
    let requestOptions: any= null;
    // fetch Token from oag and add the Authorisation Header
    this.fetchToken().subscribe((token: IOagAccessTokenResponse) => {
        let finalOptions: any= {};
        if (options) {
            finalOptions = options;
        }

        if (!finalOptions.headers) {
            finalOptions.headers = new HttpHeaders();
        }
        // first remove Header and after this add the new header
      finalOptions.headers = finalOptions.headers.delete('Authorization');

     requestOptions = finalOptions;

    }), (e): any=> {

  };

  response.next(requestOptions);

}

Here is the subscribe call to this function :

this.fetchOptions(options).subscribe((requestOptions: any) => {
    .....
}

I keep getting the following error in Internet Explorer and Chrome:

ERROR TypeError: Cannot read property 'subscribe' of undefined

What am I doing wrong?

Upvotes: 0

Views: 245

Answers (1)

Andrutto
Andrutto

Reputation: 1

It won’t work because fetchOptions method is not returning an Observable. Actually it does not return anything.

When you return an Observable from fetchOptions method you will be able to subscribe to it.

Upvotes: 0

Related Questions