Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'catch' does not exist on type 'Observable<IEmployee[]>' [duplicate]

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.

like image 463
QOnly Avatar asked Nov 02 '18 10:11

QOnly


1 Answers

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

like image 62
Muhammed Albarmavi Avatar answered Nov 09 '22 20:11

Muhammed Albarmavi