Sanity query returns null in getStaticProps when destructuring currentPost

8 replies
Last updated: Nov 29, 2025
Has anyone gotten
error - pages/[slug].tsx (171:10) @ getStaticProps
TypeError: Cannot destructure property 'currentPost' of '(intermediate value)' as it is null.
  169 |   })
  170 |
> 171 |   const { currentPost, previousPost, nextPost } = await getClient().fetch(
      |          ^
  172 |     postsTest,
  173 |     {
  174 |       slug: params?.slug,
using NextJS the call in my
getStaticProps
looks like
const { currentPost, previousPost, nextPost } = await getClient().fetch(
    postsTest,
    {
      slug: params?.slug,
    }
  )
and that
postsTest
query looks like
export const postsTest = groq`
*[_type == "post" && slug.current == $slug]{
  "currentPost": {
    ${postFields}
  },
  "previousPost": *[_type == "post" && ^.publishedAt > publishedAt]|order(publishedAt desc)[0]{title,slug},
  "nextPost": *[_type == "post" && ^.publishedAt < publishedAt]|order(publishedAt asc)[0]{title,slug}
}|order(publishedAt)[0]
`

AI Update

I've seen this error before! The issue is with how your GROQ query is structured. Your query is returning null when no matching post is found, and then you're trying to destructure properties from null, which causes the error.

The problem is in your query structure. You're filtering for posts matching the slug, then trying to project currentPost, previousPost, and nextPost for each result, and finally taking [0]. When there's no post with that slug, the filter returns an empty array, and [0] on an empty array gives you null.

Here's how to fix it:

*[_type == "post" && slug.current == $slug][0]{
  "currentPost": {
    ${postFields}
  },
  "previousPost": *[_type == "post" && ^.publishedAt > publishedAt]|order(publishedAt desc)[0]{title,slug},
  "nextPost": *[_type == "post" && ^.publishedAt < publishedAt]|order(publishedAt asc)[0]{title,slug}
}

The key change is moving [0] before the projection {} instead of after. This way:

  • You first get the matching post document (or null if none exists)
  • Then you project the fields you want

Now in your getStaticProps, you should also add a null check:

const result = await getClient().fetch(postsTest, {
  slug: params?.slug,
})

if (!result) {
  return {
    notFound: true,
  }
}

const { currentPost, previousPost, nextPost } = result

This will properly handle cases where someone navigates to a slug that doesn't exist, returning a 404 instead of crashing.

The destructuring error happens because when your query returns null, JavaScript can't destructure properties from it. By checking for null first and returning notFound: true, Next.js will show a 404 page instead of throwing an error.

Show original thread
8 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?