diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 9736bd8..003dca2 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -31,12 +31,15 @@ export class HeroService { ); } + + + /** GET hero by id. Will 404 if id not found */ getHero(id: number): Observable { - // For now, assume that a hero with the specified `id` always exists. - // Error handling will be added in the next step of the tutorial. - const hero = HEROES.find(h => h.id === id)!; - this.messageService.add(`HeroService: fetched hero id=${id}`); - return of(hero); + const url = `${this.heroesUrl}/${id}`; + return this.http.get(url).pipe( + tap(_ => this.log(`fetched hero id=${id}`)), + catchError(this.handleError(`getHero id=${id}`)) + ); } /** Log a HeroService message with the MessageService */