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
datais not an array. To be sure, you could put aconsole.log(data)just below the lineconst data = await res.json();. Could you update the question with this information?– Danizavtz
[ { _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' } ]
– Marcio Campos
The error is not in this code, it is in the route:
/blogs/categorias/[slug]. Where is this call made? Where is the variable declaredslugwho is on this call?– Danizavtz