1

How would you catch the error in that case:

   getStuff(): Observable<Stuff[]> {

    return this.http.get(url)
      .map((res: Response) => {
        return res.json()
        .map(item => {
          return {
            id: item.id
            name: item.code
          };
      });
    });

  }

I tried to put .catch() but saying that return type is not matching Supplied parameters do not match any signature of call target.

getStuff(): Observable<Stuff[]> {

    return this.http.get(url)
      .map((res: Response) => {
        return res.json()
        .map(item => {
          return {
            id: item.id
            name: item.code
          };
      });
    })
    .catch();

  }

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

4

1 回答 1

1

你可以使用Observable.throw

function handleError(error: any) {

  let errorMsg = error.message || `Yikes! There was was a problem `;
  console.error(errorMsg);

  // throw an application level error
  return Observable.throw(errorMsg);
}

并使用

 .catch(handleError);
于 2017-05-19T04:30:35.063 回答