【问题标题】:Rxjs map() function's parameter is null [duplicate]Rxjs map() 函数的参数为​​空 [重复]
【发布时间】:2019-10-06 17:19:29
【问题描述】:

我在Angular 中有以下INterceptor

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

}

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

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

我的问题是,.pipe(map(x =&gt; {...} x 参数为空。

我正在从Interceptor 返回HttpResponse,但在map()userService 中没有收到回复。

【问题讨论】:

  • 我想.pipe(map(x =&gt; { return (x instanceof HttpResponse &amp;&amp; x.status==204)?true:false }))
  • @mbojko 谢谢。我完全忘了告诉 Angular 我想要完整的回复 { observe: 'response' }

标签: angular rxjs angular7


【解决方案1】:

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

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

【讨论】:

  • return of(evt); 不会做任何事情,但管道中的下一个操作员/订阅者无论如何都会获得未更改的流。
  • 没有。我需要在我的 http 调用选项中添加 { observe: 'response' } 以告诉 Angular 我想要完整的 HttpResponse 对象
  • tap 不返回 void,它只是监听流,根本不修改它
猜你喜欢
  • 2018-08-16
  • 2021-09-27
  • 2023-03-26
  • 2020-11-02
  • 2020-08-24
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多