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.

  1. you trying assign promise array, have assignment in callback of promise.
  2. your then call on promise not return anything, call foreach iterates on collection returns nothing. if want create/return use map creates new collection based on passed in predicate.
  3. use arrow function promise callback have reference this should need it.
  4. 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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -