13

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);
    }
  }
Pavel Chuchuva
  • 22,633
  • 10
  • 99
  • 115
Jorawar Singh
  • 7,463
  • 4
  • 26
  • 39

3 Answers3

27

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:

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • 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. – Jorawar Singh Jul 17 '16 at 21:10
  • @Thierry, apparently importing the entire rxjs/Rx is not good practice. Do you think it's not that much of an improvement to import only the operators that you need? – Adam Mendoza Nov 06 '17 at 23:42
0

I had the same issue but in my case the problem was, I had import Required modules several times in Module.ts

0

The 'O' character in 'rxjs/Observable' must be Upper Case.

Specifying it in lower case 'o', will give you this and other spooky errors. This must be imported like this:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

It took me an hour to find it. When using import, generally, the names are in lower case. This is the first time I'm seeing this. I hope it will help the others :)

Mehmet Recep Yildiz
  • 1,359
  • 17
  • 14