Angular 4 Http Interceptor: next. handle (do). do no es una función


Creé este HttpInterceptor para poder manejar mejor los errores http, estaba funcionando bien antes de hacer un git pull y ejecutar la instalación de npm.

Este es mi código:

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from "rxjs";
import {ToasterService} from "angular2-toaster";

@Injectable()
export class GobaeInterceptor implements HttpInterceptor {
    constructor(private toasterService: ToasterService){
    }
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req)
            .do(event => {
                if (event instanceof HttpResponse) {
                    let response = event.body;
                    if(response.Error){
                        this.toasterService.pop('error', 'Error '+response.Code, response.Message);
                    }
                }
            });
    }
}

Y este es el error que obtengo:

TypeError: next. handle (do). do no es una función en GobaeInterceptor.webpackJsonp.../../../../../src/app / services / gobae.interceptor.ts.GobaeInterceptor.interceptar (gobae.interceptor.ts: 12) en HttpInterceptorHandler.webpackJsonp.../../../common/@angular/common/http.es5.js.HttpInterceptorHandler.handle (

¿Ha cambiado algo que pueda afectar a mi código últimamente? ¿qué puedo hacer ahora para "capturar" la respuesta http en mi interceptor?

Author: Petter Friberg, 2017-08-23

4 answers

Este error se produce porque le falta el operador do. La siguiente importación con parche el objeto observable con el operador do.

import 'rxjs/add/operator/do';

RxJS no viene incluido con todas las funciones del operador por defecto para reducir el tamaño de la biblioteca. Debe importar los operadores que desea utilizar individualmente.

 44
Author: user8510992,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-07 01:15:39

Rxjs 6 / angular 6 necesitará la tubería

return next.handle(req).pipe(
  tap(event => {
    if (event instanceof HttpResponse) {
      ...
    }
  })
);
 10
Author: Shawn Palmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-02 15:35:03

Tienes que usar import.

import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/Observable';
import 'rxjs/add/observable/throw';
 6
Author: Hebert Godoy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-09 08:35:04

Terminé cambiando el operador de esto:

next.handle(req).do

A esto:

next.handle(req).subscribe

Parece que:

  1. Los operadores de rxjs ya no se cargan por defecto

  2. Dado que Angular ha implementado observables en llamadas HTTP, subscribe es el correcto para ir

 0
Author: Multitut,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-12 15:05:48