gpt4 book ai didi

angular - 错误处理Angular 6

转载 作者:行者123 更新时间:2023-12-03 08:10:43 25 4
gpt4 key购买 nike

从后端收到409错误时,我想显示后端开发人员提供给我的响应消息。

我做这个

return this.http.post('url to API', data, {
reportProgress: true,
observe: 'events'})
.pipe(
map(
res => {
return res;
}
),
catchError((error) => {
return throwError(error);
})
);

这是我的进口
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map, catchError } from 'rxjs/operators';
import { environment } from '../../environments/environment';
import { Observable, Observer, throwError } from 'rxjs';
import { AuthService } from '../components/auth/auth.service';

但我在控制台中得到了
typeError: _node_modules_rxjs__WEBPACK_IMPORTED_MODULE_3__.Observable.throw is not a function
at CatchSubscriber.selector (http-load.interceptor.ts:21)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:43)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:132)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.error (Subscriber.js:106)
at XMLHttpRequest.onLoad (http.js:1849)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4053)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:496)

最佳答案

好的,我发现了问题

我做了一个拦截器来处理响应和错误,但我完全忘记了。我用它

return Observable.throw(error);

我应该用过
return throwError(error);

关于angular - 错误处理Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51855100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com