);
}
+ /** GET hero by id. Will 404 if id not found */
getHero(id: number): Observable<Hero> {
this.log(`requested hero id=${id}`);
- const found: Hero | undefined = HEROES.find(hero => hero.id === id);
- if (found === undefined) {
- return EMPTY;
- } else {
- return of(found);
- }
+ const url = `${this.heroesUrl}/${id}`;
+ return this.http.get<Hero>(url).pipe(
+ tap(_ => this.log(`fetched hero id=${id}`)),
+ catchError(this.handleError<Hero>(`getHero id=${id}`))
+ );
}
/**
this.log(`${operation} failed: ${error.message}`);
// Let the app keep running by returning an empty result.
- return of(result as T);
+ if (result === undefined) {
+ return EMPTY as Observable<T>;
+ } else {
+ return of(result as T);
+ }
};
}