Next.js Discord

Discord Forum

Help with displaying json data from public api

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I am trying to just simply display data from a public api (in this case nfl teams) and I can't seem to get past this Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead. error. This is super simple code and I am sure that it's really easy to fix, but can someone help me to get started on this?

Here is my code:
export default async function Home({data}) {
  return (
    <>
      <div>
        {data.sports[0].id}
      </div>
    </>
  )
}

export const getServerSideProps = async () => { 
  const query = await fetch('https://site.api.espn.com/apis/site/v2/sports/football/nfl/teams');
  const data = await query.json();
  return { 
    props: { 
      data
    }
  }
}

Let me know if there is any other info you need.
Answered by Ray
remove the async
View full answer

7 Replies

export default function Home({data}) {
  return (
    <>
      <div>
        {data.sports[0].id}
      </div>
    </>
  )
}
remove the async
Answer
West African LionOP
bruh
since you are using getServerSideProps so I assume you are using page router
the page component cannot be async in page router
West African LionOP
gotcha
I knew it would be simple, thank you so much