My getHeroes function is supposed to return Hero[] Objects, but I cannot access its methods.
Am I doing something wrong ?
hero.ts
export class Hero {
id: number;
name: string;
getName(): string {
return this.name;
}
}
heroes.service.ts
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
.pipe(
catchError(this.handleError('getHeroes', []))
);
}
heroes.component.ts
getHeroes(): void {
this.heroesService.getHeroes()
.subscribe(heroes => {
this.heroes = heroes;
this.heroes.forEach((hero) => console.log(hero));
this.heroes.forEach((hero) => console.log(hero.getName())); //ERROR here
});
}
I'm getting a ERROR TypeError: hero.getName is not a function on the last line.
Here is a live version Live link