Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: Argument of type '(error: any) => void' is not assignable to parameter of type [duplicate]

Following Code:

private getJSON(): Observable<any> {
    return this.http.get('./reportNav-NEW.json')
      .map((res:any)=> res.json())
      .catch((error:any) => console.log(error));
  }

The Error gives me:

Argument of type '(error: any) => void' is not assignable to parameter of type '(err: any, caught: Observable) => ObservableInput<{}>'. Type 'void' is not assignable to type 'ObservableInput<{}>'.

It happens in the .catch((error:any) => console.log(error));

like image 872
its.david Avatar asked Nov 27 '17 19:11

its.david


1 Answers

This worked for me (thanks to @trevor in the comments)

private getJSON(): Observable<any> {
    console.log(document.location.href);
    return this.http.get('...')
      .map((res:any)=> res.json())
      .catch((error:any) => {
        return Observable.throw(error);
      })
  }

update: Now with rxjs 6 you have to call "throwError" operator from "rxjs/operators", and use it instead of "Observable.throw".

like image 69
its.david Avatar answered Oct 18 '22 01:10

its.david