angulartypescriptasp.net-coreangular-httpclient

How to map fetched JSON to the existing object in TypeScript


I have a problem with mapping fetched JSON from the API to an existing object. TypeScript code:

enter image description here

Hero Interface:

export interface Hero {
   id: number;
   name: string;
}

Console log:

enter image description here

this.hero is always empty (undefined). How to solve this PS. I'm new in Angular and typescript.


Solution

  • It looks like the return type is an array. So you probably need to access the first element (or similar).

    this.hero = fetchedHero[0];