Rxjs map() function's parameter is null

M

monstertjie_za

Guest
I have the following INterceptor in Angular.

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//handle response
return next.handle(req).pipe(
tap(evt => {
if (evt instanceof HttpResponse) {
if (evt.status == 201) {
this.toastrService.success('Successfully Created',
'Success', this.toastConfig);
}
if (evt.status == 202) {
this.toastrService.success('Successfully Updated',
'Success', this.toastConfig);
}
if (evt.status == 204) {
this.toastrService.success('Successfully Deleted',
'Success', this.toastConfig);
}
}
return of(evt);
}),
catchError((err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status == 401) {
let strMessage = err.error ? err.error.Message ? err.error.Message : "Authorization Error: You are not allowed to access the requested resource" : "Authorization Error: You are not allowed to access the requested resource";
this.toastrService.warning(strMessage, "Authentication Response", this.toastConfig);
} else if (err.status == 500) {
this.toastrService.error("Oops! Somethign went wrong.", "Internal Error", this.toastConfig)
} else {
this.toastrService.error(err.message, "Error", this.toastConfig);
}
}

return of(err);
})
);
}


Then, I have a userService with the following deleteUser() function:

public deleteUser(id: number): Observable<boolean> {
return this.httpClient.delete<any>(environment.endpoints.user.deleteUserById(id))
.pipe(map(x => {
if (x instanceof HttpResponse) {
return x.status == 204 ? true : false;
}
}))


}

I execute the deleteUser() function by subscribing to it as follow from within a component:

deleteEvent(id: number): void {
this.userService.deleteUser(id)
.subscribe(x => {
if (x) {
this.search(); //refreshes list
}
});
}


My problem is, the .pipe(map(x => {...} x parameter is null.

I am returning the HttpResponse from the Interceptor, but there is no response received in my userService in the map().

Continue reading...
 
Top