GROQ query error: param $slug referenced but not provided

7 replies
Last updated: Mar 12, 2023
This request:`const query = groq`*[_type == "release" && slug.current == $slug][0]`
Gives me this error response:

{"error":{"description":"param $slug referenced, but not provided","end":44,"query":
What am I doing wrong?
AI Update

The error you're seeing happens because you're referencing a parameter $slug in your GROQ query but not providing its value when executing the query.

When you use parameters in GROQ (anything starting with $), you need to pass the actual values as a second argument to your fetch() call. Here's how to fix it:

const query = groq`*[_type == "release" && slug.current == $slug][0]`

// Pass the parameter value as the second argument
const result = await client.fetch(query, { slug: 'your-slug-value' })

The parameters object maps the parameter name (without the $) to its value. So $slug in your query corresponds to { slug: 'value' } in your parameters.

If you're getting the slug from somewhere like a URL parameter or props, it would look like:

// From Next.js route params
const result = await client.fetch(query, { slug: params.slug })

// Or from a function parameter
async function getRelease(slugValue) {
  return await client.fetch(query, { slug: slugValue })
}

Using parameters this way is the recommended approach because it's safer than string interpolation - the Sanity client automatically handles proper encoding and prevents GROQ injection issues. Parameters must be JSON-serializable values, which means they can't contain GROQ expressions, making them safe to use with user input.

Show original thread
7 replies
Try change it to this:

const $slug = '/mySlug'

const query = `*[_type == "release" && slug.current == "${slug}"][0]
Where you set $slug to the slug you want to get.
I'm really confused about all the references in the groq query and getStaticProps and getStaticPaths..
Like:
`const query = groq`*[_type == "release" && release.catalog_number == $slug][0]`
Type is ok. but after the &&, the first one is the property from the groq that I want the slug to be set from right? And after the second == the $ references a variable by this name outside the groq query? Am I getting it right?
..and then when you get to getStaticProps, you need the params from the getStaticPaths query? Just to let getStaticProps know which document you are fetching right? ..and then, the client.fetch .... the first parameter is simple, but the second one? Does it tell the query what document to get? Why/how? I don't get it..
Here's a working example from my practice project. This is using SSR though but it should be easy enough to modify it to use static props:

export const getServerSideProps: GetServerSideProps  = async (pageContext: GetServerSidePropsContext) => {
  const pageSlug: string | string[] | undefined = pageContext.query.slug

  if (!pageSlug) {
    return {
      notFound: true
    }
  }

  // eslint-disable-next-line @typescript-eslint/typedef
  const query = `*[_type == 'post' && slug.current == "${pageSlug}"] {
    _id,
    title,
    author-> {
      name,
      image
    },
    description,
    "mainImageDimensions": mainImage.asset->metadata.dimensions,
    "mainImageUrl": mainImage.asset->url,
    mainImage,
    slug,
    body
  }`

  try {
    const post: PostType[] = await sanityClient.fetch(query)

    if (!post || post.length === 0) {
      return {
        notFound: true
      }
    }

    return {
      props: {
        post: post[0]
      }
    }
  } catch (error) {
    console.error(error)
    return {
      notFound: true
    }
  }
}
user U
first argument in sanity client
fetch
method takes the GROQ query. In your example,

const query = groq`*[_type == "release" && slug.current == $slug][0]

$slug
here will be replaced with 2nd parameter you pass in.

sanityClient.fetch(query, { slug })
For example, if the
slug
has a value of
post-1

Then the query above will be translated to:


*[_type == "release" && slug.current == 'post-1'][0]
And the slug here:
sanityClient.fetch(query, { slug })
..comes from getStaticPaths and exported params? Or?
Why does it have {}?

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?