As written above, I want to use a Service, which is injected in root, in a function to logout the user after an 401 error. The function is called in a pipe after an error occured in one of my http requests. Unfortunately I get an error like:
ERROR TypeError: Cannot read property 'logout' of undefined
after an 401 error. It seems like the function is called in a different scope. I tried to inject the Service via injector.get, to set my service and the error function to public but nothing worked.
Is there any other opportunity to fix this?
constructor(private httpClient: HttpClient, private authenticationService: AuthenticationService) {}
private handleError(error: HttpErrorResponse) {
if (error.status === 401) {
this.authenticationService.logout();
}
return throwError('Something bad happened; please try again later.');
}
getSomething(): Observable<HttpResponse<Model>> {
return this.httpClient.get<Model>(this.apiEndpoint, {
observe: 'response'
}).pipe(
catchError(this.handleError)
);
}