angular - Rxjs map() 函数的参数为空
问题描述
我有以下内容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
map()
解决方案
我认为您的问题是您在tap
运营商内返回。
据我记得,tap
总是返回void
,这可能是一个问题,不是吗?
推荐阅读
- amazon-web-services - Lex:Lambda 无法委派
- c# - 如何删除水晶报表中详细信息部分顶部的空白
- npm - npm 错误:https://registry.npmjs.org/native-url 处的 json 响应正文无效 原因:JSON 输入意外结束
- python - 我想创建 Django 和 React 音乐应用程序,但 spotify api 似乎无法正常工作,我也尝试使用 vpn 但没有响应
- node.js - 如果 findOne() 没有返回文档,如何向 MongoDB 添加新文档?
- xquery - 为什么第一个 xquery 语句有效但第二个无效
- python - 对Dataframe的行和列进行排序
- delphi - Delphi.Personality 不可用
- python-3.x - GPIO.add_event_detect() 正在检测连续按下错误按钮
- sql - MSSQL Query 独立工作,但不能作为存储过程