Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 crash 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);
    }
  }
share|improve this question
up vote 3 down vote accepted

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:

share|improve this answer
    
It worked! thanks. it's crazy that i have this import 'rxjs/Rx in my vendors file where i import my other dependencies also for angular2, but i believe i need to import this library to each file where i want to use this or in the main.ts. – MrJSingh Jul 17 '16 at 21:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.