Nextjs Error occurred prerendering pages

Asked

Viewed 102 times

1

 Export encountered errors on following paths:
        /blogs/categorias/[slug]
TypeError: Cannot read property '0' of undefined
  export const getStaticPaths = async () => {
  const res = await fetch(`api/categorias/`);
  const postAwait = await res.json();
  
  const paths = postAwait.map(node => {
    return { params: { slug: node.slug }}
  });

  return {
    paths,
    fallback: true,
  }
  
}


export const getStaticProps = async (context) => {
  const { slug } = context.params;  
  const res = await fetch(`api/categorias?slug=${slug}`);
  const data = await res.json(); 

  return {
    props: {
      singlePost: data[0]
    },
    revalidate: 1,
  };
}

console.log on date: i want to return what is inside blogs but for that I need to put date[0] and that’s what nextjs is bugging

[
  {
    _id: '601006079a0c1122f4cea4a6',
    nome: 'Notícias',
    published_at: '2021-01-26T12:42:12.473Z',
    slug: 'noticias',
    createdAt: '2021-01-26T12:07:35.163Z',
    updatedAt: '2021-01-26T12:42:12.536Z',
    __v: 0,
    blogs: [ [Object] ],
    id: '601006079a0c1122f4cea4a6'
  }
]
  • Probably the data that is in the variable data is not an array. To be sure, you could put a console.log(data) just below the line const data = await res.json();. Could you update the question with this information?

  • [ { _id: '601006079a0c1122f4cea4a6', name: 'News', published_at: '2021-01-26T12:42:12.473Z', Slug: 'news', createdAt: '2021-01-26T12:07:35.163Z', updatedAt: '2021-01-26T12:42:12.536Z', __v: 0, blogs: [ [Object] ], id: '601006079a0c1122f4cea4a6' } ]

  • The error is not in this code, it is in the route: /blogs/categorias/[slug]. Where is this call made? Where is the variable declared slug who is on this call?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.