Rxjs map()函数的参数为​​null

时间:2019-10-06 17:19:29

标签: angular rxjs angular7

我在INterceptor中有以下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);
        })
    );
}

然后,我有一个userService,具有以下deleteUser()函数:

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;
    }
  }))

}

我通过deleteUser()对组件执行subscribing函数,如下所示:

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

我的问题是.pipe(map(x => {...} x参数为空。

我正在从HttpResponse返回Interceptor,但是在userService的{​​{1}}中没有收到响应。

1 个答案:

答案 0 :(得分:-1)

我认为您的问题是您在tap运算符内返回。

据我所记得,tap总是返回void,这可能是个问题,不是吗?