Getting the following error in Next.js - A required parameter (id) was not provided as a string in getStaticPaths
While using getStaticPath in Next.js pages I was getting the above error. Here is my method,
export async function getStaticPaths() {
const res = await fetch('https://jsonplaceholder.typicode.com/users')
const userList = await res.json()
let params = userList.map((user:any) => {
return {
params: {id : user.id}
}
})
return {
paths: params,
fallback: false,
}
}
The issue was user.id
needs to be string as pointed by the error. The solution is to convert user.id
to user.id.toString()
.
export async function getStaticPaths() {
const res = await fetch('https://jsonplaceholder.typicode.com/users')
const userList = await res.json()
let params = userList.map((user:any) => {
return {
params: {id : user.id.toString()}
}
})
return {
paths: params,
fallback: false,
}
}