How do we create multiple collections using Angular-in-memory-web-api? Not an issue with single collection. But I\'m not able to implement it f
Adding to this discussion, I had to use Partial as it kept on saying I was missing an id when completing the Tour of Heroes Lesson 06. I used the message service to see how far the code went before it stopped working, so you can ignore the this.log lines, but you can add them to your own project to help with debugging. First, I updated the addHero as follows:
addHero(hero: string): void {
this.log(`In addHero of HeroesComponent, name is: ${hero}`);
if (!hero)
{
this.log(`In if(!hero.name) of add hero: ${hero}`);
return;
}
this.heroService.addHero({ name: hero } as Partial)
.subscribe(hero => {
this.heroes.push(hero);
});
}
And then I updated the heroes.service.ts file to use the partial hero and allow the code to generate its own id from the in-memory-data.service.ts:
addHero(hero: Partial): Observable {
this.log(`In addHero: heroeName is: ${hero.name}`);
return this.http.post(this.heroesUrl, hero, this.httpOptions).pipe(
tap((newHero: Hero) =>
this.log(`added hero with string id=${newHero.id}`)),
catchError(this.handleError("Error in addHero")));
}
As far as I can see, it is now working as expected. There may be some more code to update, but this should help anyone with the following error message:
ERROR in src/app/heroes/heroes.component.ts:52:30 - error TS2352: Conversion of type '{ name: never; }' to type 'Hero' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Property 'id' is missing in type '{ name: never; }' but required in type 'Hero'.