Search code examples
angularangular-cliangular9angular-cli-v9

Angular9: Type 'Observable<HttpEvent<class>>' is not assignable to type 'Observable<class>'


In my angular application, I have a service file that makes API calls. in one of the function I have as below.

 getSelectedPractitionerPermissions(id): Observable<classA> {
    const url =  apiUrl
    this.options = this.authService.getAuthInfo();
    return this.http.get<classA>(url, this.options);
  }

But I am getting an error as below

Type 'Observable<HttpEvent>' is not assignable to type 'Observable'. Type 'HttpEvent' is not assignable to type 'ClassA'. Type 'HttpSentEvent' has no properties in common with type 'ClassA'.

I have all the required headers and token I get from getAutInfo

  getAuthInfo() {
    const bearerToken = localStorage.getItem('token');
    let headers: HttpHeaders = new HttpHeaders();
    headers = headers.append('Authorization', 'Bearer ' + bearerToken);
    headers = headers.append('Cache-control', 'no-cache');
    headers = headers.append('Cache-control', 'no-store');
    headers = headers.append('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT');
    headers = headers.append('Pragma', 'no-cache');
    return { headers };
  }

If I remove the this.options in the GET route, it works fine. But I wanted to have them as I need to pass token and other parameters.


Solution

  • In getSelectedPractitionerPermissions

    return this.http.get<classA>(url, {headers: this.options });
    

    and in getAuthInfo:

    return headers;