Angular2 Http with RXJS Observable TypeError: this.http.get(...).map(...).catch is not a function

Jorawar Singh

I have following Service which was working fine until today i got this error

TypeError: this.http.get(...).map(...).catch is not a function. 

When I'm debugging this code it crashes when it comes to catch method.

import { Test } from "./home.component";
import { Injectable }     from "@angular/core";
import { Inject } from "@angular/core";
import { Http , Response  } from "@angular/http";
import { Observable }     from "rxjs/Observable";

@Injectable()
export class HomeService {
   public constructor(@Inject(Http)  private http: Http) {}

   public getData (): Observable<Test []> {
        return this.http.get("./src/app/home/home-data.json")
            .map(this.extractData).catch(this.handleError);
    }

    public extractData(res: Response) {
        let body = res.json();
        return body.data || { };
    }

    public handleError (error: any) {
        // In a real world app, we might use a remote logging infrastructure
        // We"d also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : "Server error";
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
  }
Thierry Templier

It seems that the catch operator isn't imported.

You could try to import it like this:

import 'rxjs/add/operator/catch'

Or more generally this if you want to have more methods for observables:

import 'rxjs/Rx';

See this question:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Observable Map function not running (Angular2, http)

Angular2 RxJs map http response

Angular2 - TypeError: this.http.get(...).toPromise is not a function

How to Map Response object returned by Http Service to a TypeScript Objects using Observable Map function in Angular2

TypeError: this.http.get(...).map is not a function

Angular2 RxJs: simple take() from http returned observable

Rxjs Observable Interval and Angular2 HTTP: wait for response

Angular2 http is missing .map function

Angular2 http.get() ,map(), subscribe() and observable pattern - basic understanding

Angular2 rxjs http.request.catch has strange behaviour for some http errors

Angular 2 http get observable

Angular2 observable http get conditional repeat

TypeError: $http.get is not a function in Angular JS

Angular 4 this.http.get(...).map is not a function

Angular 2 & RxJs catch function callback binding to 'this' causes http request to be repeated over and over

Angular, rxjs: HTTP Observable depending authenticated user

Angular/RxJS wait for HTTP response in inner Observable

Angular2 - http.post(...).map is not a function

Angular2 / RxJS - updating variable after getting data from Http observable

angular2 filter/map results of http get into variables

Rxjs. Map each object Observable<Foo[]> using http call

this.http.get(...).map is not a function

Http Get catch is not working in angular 2

Angular 2 and RxJS: How to change HTTP call that returns Observable

Angular 2 http get observable called twice

Where is the http.get() function in Angular2 defined?

Angular RxJs how loop through array and assign http get requests to a single reactive observable

trouble using rxjs with http observable

angular observable http call map response to interface