api - Angular Promise<void>' is not assignable to type -
i building angular4 website using contentful cms api retrieve data. problem cannot assign right types returned data thought console shows types.
the mock data:
export const names: page[] = content.getentries({ content_type: 'mainmenu' }).then(function(response){ response.items.foreach(element => { return element.fields; }) });
which return via console (if used console.log ) :
object { title: "about aliens" } object { title: "portfolio" } object { title: "meet team" } object { title: "contact us" }
and class use assign data types :
export class page { title: string; }
i new typescript , want know got wrong , appreciate if guided me can go master returning such data api.
thank you.
- you trying assign promise array, have assignment in callback of promise.
- your
then
call on promise not return anything, callforeach
iterates on collection returns nothing. if want create/return usemap
creates new collection based on passed in predicate. - use arrow function promise callback have reference
this
should need it. - there no need make page concrete type, use interface instead can use casting. need concrete type if going add behavior if plain old object deserialized json use interface.
the fix:
export names: ipage[]; // no assignment @ point
execute getentries within method.
content.getentries({ content_type: 'mainmenu' }).then((response) => { names = response.items.map(element => element ipage); });
ipage
export interface ipage { title: string; }
Comments
Post a Comment