undefined PATHS in getStaticPaths Next JS

Viewed 1829

I'm trying to do getStaticPaths and getStaticProps but I'm always getting an error that the paths is undefined in getStaticPaths. Next version is currently on 10.0.3.

This is the error message

Error: Invalid value returned from getStaticPaths in /properties/[id]. Received undefined Expected: { paths: [], fallback: boolean }

Here's my code

export async function getStaticPaths() {
  try {
    const properties = await api.get('/properties', {
      headers: generateAuthHeaders(null, 'application/json', API_TOKEN)
    })

    const paths = properties.res.data.propertyDetails.map(value => {
      return {
        params: { id: value.id }
      }
    })

    return { paths, fallback: false }
  } catch (er) {
    //  do nothing
  }
}

export async function getStaticProps({ params }) {
  try {
    const properties = await api.get(`/properties/${params.id}`, {
      headers: generateAuthHeaders(null, 'application/json', API_TOKEN)
    })

    return {
      props: {
        propertyDetails: JSON.parse(JSON.stringify(properties.data.properties))
      }
    }
  } catch (er) {
    return {
      props: { propertyDetails: false }
    }
  }
}
1 Answers

My guess from looking at the code is that the catch block in getStaticPaths is being entered, and as it has no return statement the function ends and returns undefined by default.

So you need to return something, like this...

export async function getStaticPaths() {
  try {
    const properties = await api.get('/properties', {
      headers: generateAuthHeaders(null, 'application/json', API_TOKEN)
    })

    const paths = properties.res.data.propertyDetails.map(value => {
      return {
        params: { id: value.id }
      }
    })

    return { paths, fallback: false }
  } catch (er) {
    console.error(er)
    return { paths: [], fallback: false } // <- ADDED RETURN STMNT
  }
}

I am not familiar with the getStaticPaths API, so you may need to return something (not an empty array) for paths. So this code may well not work, but I'm just pointing out that you need to return an object in the form specified in the error message you are receiving.

Related