12

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));

its.david
  • 2,115
  • 5
  • 16
  • 31
  • @Carcigenicate I read that console.log not only logs it to the console but also provides the return capability. It was explained in the last comment of the correct answer of this issue here: https://stackoverflow.com/questions/39406043/how-to-fetch-json-file-in-angular-2 – its.david Nov 27 '17 at 20:02

1 Answers1

18

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".

Olivier Cadoz
  • 116
  • 1
  • 6
its.david
  • 2,115
  • 5
  • 16
  • 31
  • 7
    What id you don't want to throw the error, but rather handle it (with a modal, etc) and then ignore it? – Shamoon May 02 '18 at 13:56