When I hover over .catch(this.errorHandler)
, an error message is displayed
Property 'catch' does not exist on type 'Observable'.ts(2339)
I am unable to import the catch function into Angular Typescript.
Upon hovering over .catch(this.errorHandler)
, the following error message appears:
Property 'catch' does not exist on type 'Observable'.ts(2339)
As suggested in another Stack Overflow post: Property 'catch' does not exist on type 'Observable<any>' I should simply add:
import 'rxjs/add/operator/catch'
I have also attempted importing
import {Observable} from 'rxjs/Rx';
and
import { catchError } from 'rxjs/operators';
and using catchError instead of catch.
However, none of these methods proved successful
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IEmployee } from './employee';
import { Observable, fromEventPattern } from 'rxjs';
import 'rxjs/add/operator/catch';
import {catchError} from "rxjs/operators"
import 'rxjs/add/observable/throw';
@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")
}
}