I'm newbie for the angular. And current I'm learning angular 6 features. When I try to handle error there is some issues like,
Module not found: Error: Can't resolve 'rxjs/add/observable/throw' in 'D:\WORKSPACE\Angular5\binding\src\app'
Module not found: Error: Can't resolve 'rxjs/add/operator/catch' in 'D:\WORKSPACE\Angular5\binding\src\app'
ERROR in src/app/employee.service.ts(20,14): error TS2339: Property 'catch' does not exist on type 'Observable'
Here is my employee.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IEmployee } from './employee';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
//It self injectable defendency.
@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  private _url: string = "/assets/data/employees.json";
  constructor(private http: HttpClient) { }
  getEmployees(): Observable<IEmployee[]>{
    return this.http.get<IEmployee[]>(this._url)
            .catch(this.errorHandler);                    
  }
  errorHandler(error: HttpErrorResponse){
      return Observable.throw(error.message || "Server Error");
  }
}
employee.ts
export interface IEmployee{
    id : number,
    name : String,
    age : number
}
I tried to solve this question using bellow link. But I couldn't get the answer for my question.
Property 'catch' does not exist on type 'Observable' and Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]
Please give me some solution. Thank you.
In angular 6 we use the rxjs 6 , your example correct for old version of angular you may need to read more about rxjs 6
try this way
  getEmployees(): Observable<IEmployee[]>{
    return this.http.get<IEmployee[]>(this._url)
               .pipe(catchError(this.handlerError));
   }
catch / catchError , RxJS 6 Changes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With