加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

typescript – Angular 5 Interceptor:TypeError:next.handle(

发布时间:2020-12-17 07:19:02 所属栏目:安全 来源:网络整理
导读:我创建了一个角度拦截器来检查我的身份令牌的有效性.不知何故,do方法不被angular识别.订阅工作,但我不想要这个解决方案,因为它将我的请求加倍发送到服务器. TypeError: next.handle(...).do is not a functionat AuthTokenService.webpackJsonp.../../../../
我创建了一个角度拦截器来检查我的身份令牌的有效性.不知何故,do方法不被angular识别.订阅工作,但我不想要这个解决方案,因为它将我的请求加倍发送到服务器.
TypeError: next.handle(...).do is not a function
at AuthTokenService.webpackJsonp.../../../../../src/app/commons/services/interceptors/auth-token.service.ts.AuthTokenService.intercept (auth-token.service.ts:37)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at XsrfService.webpackJsonp.../../../../../src/app/commons/services/interceptors/xsrf.service.ts.XsrfService.intercept (xsrf.service.ts:15)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at HttpXsrfInterceptor.webpackJsonp.../../../common/esm5/http.js.HttpXsrfInterceptor.intercept (http.js:2489)
at HttpInterceptorHandler.webpackJsonp.../../../common/esm5/http.js.HttpInterceptorHandler.handle (http.js:1796)
at MergeMapSubscriber.project (http.js:1466)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.webpackJsonp.../../../../rxjs/_esm5/Subscriber.js.Subscriber.next (Subscriber.js:92)

这是我的拦截器代码:

import { Injectable,NgModule} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpEvent,HttpInterceptor,HttpHandler,HttpRequest} from 
'@angular/common/http';
import { SessionService } from 'app/commons/services/auth/session.service';
import { HttpErrorResponse } from "@angular/common/http";
import { StateService } from '@uirouter/angular';

import 'rxjs/add/operator/do';

import * as _ from "lodash";

@Injectable()
export class AuthTokenService implements HttpInterceptor {
  constructor(
    private sessionService: SessionService,private stateService: StateService
  ) {}

  intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {

    const currUser = this.sessionService.getCurrentUser();
    const authToken = _.get(currUser,['auth_token'],null);

    let dupReq = req.clone({ headers: req.headers.set('Authorization','') });

    if (!_.isNil(authToken)) {
      dupReq = req.clone({ headers: req.headers.set('Authorization',`Token ${authToken}`) });
    }

    return next.handle(dupReq)
      .do(ev => {
        console.log(event);
      })
  }
};

我不认为我错过任何东西,但由于某种原因,它不会有in the guide提到的副作用

在这里发现我的错误.在角度5中,操作符被改为可租赁操作符.我不太了解他们做的事情,因为我是新手使用这种技术.但在看了Angular 4文档和关于拦截器如何工作的答案几个小时的完全沮丧之后,我终于看到了这篇文章: Angular 5: Upgrading & Summary of New Features

我的更新代码:

import { map,filter,tap } from 'rxjs/operators';

@Injectable()
export class AuthTokenService implements HttpInterceptor {
    intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {

        const started = Date.now();
        return next.handle(dupReq).pipe(
        tap(event => {
          if (event instanceof HttpResponse) {
            const elapsed = Date.now() - started;
            console.log(`Request for ${req.urlWithParams} took ${elapsed} ms.`);
          }
        },error => {
          console.error('NICE ERROR',error)
        })
      )
    }
}

从我的http请求中捕获错误,如魅力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读