Next.js Discord

Discord Forum

getStaticProps keeps on returing undefined

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
So I noticed that getStaticProps wasn't returing my fetch request, so to make sure that it is working I made it have a single variable returned

so it currently looks like

export async function getStaticProps(){
  const repo = "Hello world"
  return { props: { repo } }

}

export default function Home({repo}) {

  console.log(repo)
return (<div> example </div>)
}


but I am still getting undefined.
Answered by @ts-ignore
if you're using app router then getStaticProps doesn't work
View full answer

7 Replies

Asiatic LionOP
I also tried following this example https://www.geeksforgeeks.org/next-js-getstaticprops-function/


export async function getStaticProps() {
 
  // Call the fetch method and passing 
  // the pokeAPI link
  const response = await fetch(
      'https://pokeapi.co/api/v2/pokemon/');

  // Parse the JSON
  const data = await response.json();

  // Finally we return the result
  // inside props as allPokemons
  return {
      props: { allPokemons: data.results },
  };
}
export default function Home({allPokemons}) {

  console.log(allPokemons)


and I am still getting undefined
Answer
@@ts-ignore if you're using `app` router then `getStaticProps` doesn't work
Asiatic LionOP
Do I have any other options?
@Asiatic Lion Do I have any other options?
you can directly perform any fetch calls or db queries in that component
@@ts-ignore you can directly perform any fetch calls or db queries in that component
Asiatic LionOP
Last question I swear, but is it worth changing to the other router?
@Asiatic Lion Last question I swear, but is it worth changing to the other router?
new router comes with many new features, you are not forced to use the new router the older pages router is still supported and will be supported for a long time
Asiatic LionOP
Thanks so much!