繁体   English   中英

获取 TypeError:在发送 ajax 请求(Angular 12)时无法读取未定义的属性(读取“管道”)

[英]Getting TypeError: Cannot read properties of undefined (reading 'pipe') while sending ajax request (Angular 12)

我在发送 ajax 请求时遇到错误,

ERROR TypeError: Cannot read properties of undefined (reading 'pipe')
at AuthTokenInterceptor.intercept (auth-token.interceptor.ts:43)

auth-token.interceptor.ts

intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> { 
    console.log('request', request); // here i am getting request data 

    // if (accessToken) {
    //   request = this.getRequestWithToken(request, accessToken);
    // }
    const handlers = next.handle(request); // but here i am getting undefined
    console.log('next.handle(request)', handlers); --> undefined

    return handlers.pipe(
      catchError((responseError) => {
        console.log('authToken responseError', responseError);
        if (responseError instanceof HttpErrorResponse) {
        } else {
          return throwError(responseError);
        }
      })
    );
  }

我有数据 reuest 但将next.handle(request)设为undefined

任何帮助,将不胜感激。 谢谢!

next.handle()返回一个 Observable。 Angular:

An interceptor may transform the response event stream as well, by 
applying additional RxJS operators on the stream returned by 
next.handle().

它是未定义的,因为它是异步的。 使用tap操作员记录它。

 return handlers.pipe(
  tap(res => console.log(res)),
  catchError((responseError) => {
    console.log('authToken responseError', responseError);
    if (responseError instanceof HttpErrorResponse) {
    } else {
      return throwError(responseError);
    }
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM