X-Git-Url: https://juplo.de/gitweb/?p=examples%2Fangular-tour-of-heroes;a=blobdiff_plain;f=src%2Fapp%2Fhero.service.ts;h=1db60726d0db05ed6dd8c5f438dfdf64faa3ab03;hp=9003fe1fb02718907bde31a3bfc4eb3bac35c734;hb=ee578cce6211199049a491e3119deba48a484478;hpb=d42007487362bd314434f11dc26c3a291e7ff4e7 diff --git a/src/app/hero.service.ts b/src/app/hero.service.ts index 9003fe1..1db6072 100644 --- a/src/app/hero.service.ts +++ b/src/app/hero.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { Observable, of } from 'rxjs'; +import { Observable, of, EMPTY } from 'rxjs'; import { Hero } from './hero'; import { HEROES } from './mock-heroes'; import { MessageService } from './message.service'; @@ -19,6 +19,11 @@ export class HeroService { getHero(id: number): Observable { this.messageService.add(`HeroService: fetched hero id=${id}`); - return of(HEROES.find(hero => hero.id === id)); + const found: Hero | undefined = HEROES.find(hero => hero.id === id); + if (found === undefined) { + return EMPTY; + } else { + return of(found); + } } }