【发布时间】:2020-12-06 04:13:50
【问题描述】:
浏览器控制台显示如下错误:
zone.js:668 Error: Uncaught (in promise): [object Undefined]
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:500)
at invokeTask (zone.js:1540)
at globalZoneAwareCallback (zone.js:1577)
我不知道在哪里查看源代码。
更新:在 Firefox 下打开的同一页面给出了消息:
Error: Uncaught (in promise): [object Undefined]
Stack trace:
resolvePromise@http://localhost:4200/polyfills.js:3131:31
makeResolver/<@http://localhost:4200/polyfills.js:3041:17
setError@http://localhost:4200/scripts.js:979:21
messageCallback@http://localhost:4200/scripts.js:1092:25
./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2738:17
./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:4200/polyfills.js:2505:28
./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:4200/polyfills.js:2813:24
invokeTask@http://localhost:4200/polyfills.js:3857:9
globalZoneAwareCallback@http://localhost:4200/polyfills.js:3883:17
zone.js:668
更新:如果错误与我新添加的拦截器源代码有关,以下是我如何处理可能的异常:
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return this.addAuthHeader(request, next);
}
private addAuthHeader(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('=======>> Intercepting the http request to add the jwt token in the header');
const tokenPromise: Promise<any> = this.keycloakClientService.getToken();
const tokenObservable: Observable<any> = Observable.fromPromise(tokenPromise);
tokenObservable.map(authToken => {
console.log('Token value: ' + authToken);
// Clone the request before it is sent to the server
// as the original request is immutable and cannot be changed
request = request.clone({
setHeaders: {
'Authorization': AUTH_HEADER_PREFIX + authToken
}
});
console.log('=======>> The request has been cloned');
});
console.log('Handle the request in the interceptor chain');
return next.handle(request)
.catch(response => {
if (response instanceof HttpErrorResponse) {
if (response.status === 401) {
// TODO redirect to the login route or show a modal
}
console.log('The error has been handled by the interceptor', response);
}
return Observable.throw(response);
})
.do((response: HttpEvent<any>) => {
if (response instanceof HttpResponse) {
console.log('The response has been handled by the interceptor', response);
}
});
}
【问题讨论】:
-
我不确定你能否在你的根模块
console.log((new Error('throw')).stack);中尝试一下constructor -
[通用评论] 当我收到不知道发生了什么的错误消息时,通常我首先尝试的是
ng serve --prod。这通常会给我在某种程度上与运行时错误相关的构建错误。 -
@Vikas 我将构造函数编码为
AppModule() { console.log((new Error('throw')).stack); },但它输出的错误完全相同。 -
请不要关闭这个问题。虽然我同意这似乎是题外话。此特定错误不是可能出现的随机错误。发生这种情况有特定的原因,我们需要将其记录在互联网上,以便人们找到解决方案。当我遇到这个错误时,你基本上没有希望解决它。直到 Angular 和 WebPack 团队修复他们的代码以使这些错误变得更好。我们真的无能为力让这个“主题化”。
-
哦哦,等等...你提到这发生在你添加了 http 拦截器之后。您是否可能没有将拦截器中的错误案例包装到
Observable.throw/throwError中,而只是将其保留为纯“错误”?您必须返回 Observable 发出的错误,而不是错误本身。参考:angular.io/guide/http#getting-error-details