Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

568
Visualizações
Next.js, getStaticProps not working. TypeError: Cannot read properties of undefined (reading 'map')

I created a project starting from create-next-app. Inside the Blog.js page I tried to fetch from https://jsonplaceholder.typicode.com/posts using the getStaticProps() function.

I used the guide https://nextjs.org/docs/basic-features/data-fetching/get-static-props#using-getstaticprops-to-fetch-data-from-a-cms. Once everything is implemented, I open the page in the browser and it tells me

Server Error
TypeError: Cannot read properties of undefined (reading 'map')

Here is the code

function Blog({ posts }) {
  return (
    <ul>
      {posts.map((post) => (
        <li>{post.title}</li>
      ))}
    </ul>
  )
}

export async function getStaticProps() {
  const res = await fetch('https://jsonplaceholder.typicode.com/posts')
  const posts = await res.json()

  return {
    props: {
      posts,
    },
  }
}

export default Blog
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you are writting the function inside of the component you want to render. The only situation you are going to do this is when you are making dynamic URLs based on some parameter, in that case you will use getStaticProps and getStaticPages mixed.

try to do something like this:

  1. Writte your Blog component in a file (Blog.tsx/jsx):

function Blog({ posts }) {
  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

export default Blog

  1. Go to the page where you will render this component (it could be an index.tsx/jsx for instance)
import type { NextPage } from "next";
import Blog from 'wherever it is'

const Home: NextPage = ({posts}) => {
  return (
    <>
          <Blog posts={posts}/>    
    </>
  );
};

export async function getStaticProps() {
  const res = await fetch('https://jsonplaceholder.typicode.com/posts')
  const posts = await res.json()
  
  return {
    props: {
      posts,
    },
  }
}


export default Home;

Ps.: when you are using map, never forget the key property ;) Hope it could help ya, hugs from Brazil.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda