I am using Strapi with Next.js for my blog project
I am trying to make dynamic pages by using [id].js inside pages/posts/[id].js
But, the problem is when I try to map through the API of Strapi inside getStaticPaths() it gives me an error with data.map is not defined
Note:- I am using NextJS V12.0.8 with Strapi V4.0.4
Below is my code
export async function getStaticPaths() {
const postsRes = await axios.get("http://localhost:1337/api/posts?populate=image");
const paths = postsRes.map((post) => {
return { params: {id: post.id.toString()} }
});
// const paths = { params: {id: '1' } }
return {
paths,
fallback: false
}
}
Complete [id].js
Page Code Link -
Error Screenshot -
I am using Strapi with Next.js for my blog project
I am trying to make dynamic pages by using [id].js inside pages/posts/[id].js
But, the problem is when I try to map through the API of Strapi inside getStaticPaths() it gives me an error with data.map is not defined
Note:- I am using NextJS V12.0.8 with Strapi V4.0.4
Below is my code
export async function getStaticPaths() {
const postsRes = await axios.get("http://localhost:1337/api/posts?populate=image");
const paths = postsRes.map((post) => {
return { params: {id: post.id.toString()} }
});
// const paths = { params: {id: '1' } }
return {
paths,
fallback: false
}
}
Complete [id].js
Page Code Link - https://pastebin.com/SnzLirys
Error Screenshot - https://prnt.sc/26ha6z5
Share Improve this question edited Jan 22, 2022 at 12:40 juliomalves 50.3k23 gold badges177 silver badges168 bronze badges asked Jan 22, 2022 at 5:47 Owaiz YusufiOwaiz Yusufi 9182 gold badges15 silver badges43 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 15All you needed to do is restart the npm run dev by pressing ctrl + c then re running the dev
postsRes.data.map()
instead? – juliomalves Commented Jan 22, 2022 at 12:39axios.get
returns aResponse
object. The data you are looking for can be found atpostsRes.data
– derpirscher Commented Jan 22, 2022 at 12:53postRes.data
is not an array but nobody can help you beyond that point, because we don't know whatpostRes.data
really is ... – derpirscher Commented Jan 22, 2022 at 14:08