Troubleshooting Next.js and Sanity integration with query error

15 replies
Last updated: May 18, 2022
Good morning! I'm trying to connect my sanity app with Next.js using createClient from "next-sanity", but I keep getting this error: "*ClientError: expected '}' following object body*"
This is my code:

import { createClient } from "next-sanity"

export const sanityClient = createClient({

dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || "production",

projectId: "ekdlavlv",

apiVersion: "2022-03-25",

useCdn: process.env.NODE_ENV === "production",

})
AI Update

Good news - your createClient configuration looks perfectly fine! The error "ClientError: expected '}' following object body" is actually coming from a malformed GROQ query, not from your client setup.

The Problem

This error almost always means there's a syntax issue in one of your GROQ queries. Based on similar cases in the Sanity community, the most common culprit is a missing comma in your query projection.

The Fix

Check your GROQ queries - especially the one in your [slug] page. Every field in a projection needs a trailing comma, including before nested projections. For example:

*[_type == "property" && slug.current == $pageSlug][0]{
  title,
  location,
  propertyType,
  mainImage,
  modalidad,  // ← Make sure this comma is here!
  owner->{
    _id,
    name,
    slug,
    image
  },
}

If you have something like:

modalidad
owner->{

Without a comma after modalidad, you'll get the "expected '}' following object body" error.

Other Common Causes

This error can also happen if you write:

  • { slug.current } instead of { "slug": slug.current } (shorthand syntax without proper field naming)
  • Unmatched braces or brackets
  • Missing quotes around custom field names

About Your Setup

The apiVersion you added is correct and necessary - that's not causing the issue. The error is thrown by Sanity's query parser when it encounters malformed GROQ syntax, so look through all your .fetch() calls and double-check the query syntax.

Your client configuration is solid, so once you fix the query syntax, everything should work!

This error typically indicates that your query is malformed. Can you share it please?
Sure! You mean this?`const query =
*[ _type == "property" && slug.current == $pageSlug][0]{

title,

location,

propertyType,

mainImage,

images,

price,

expensas,

ambientes,

bathrooms,

descripcion,

modalidad

owner->{

_id,

name,

slug,

image

},
`}``
It’s actually looking okay. Do you have another query maybe?
I have this in my index.js, but this works fine:
export const getServerSideProps = async () => {
`const query = `*[_type == "property"]``

const properties = await sanityClient.fetch(query)

...
I also had this error: "ClientError: Must be an attribute or a string key", but I read that I have to write the apiVersion, and when I do that, the "*ClientError: expected '}' following object body*" appears. I'm not sure what to do
This error is also indicator of a malformed query.
This happens when you do something like:
{ slug.current }
Instead of:

{ "slug": slug.current }
Maybe it's because of this?
No, this is definitely a query error. It’s thrown by Sanity. It’s not related to what you return to your component. 🙂
Need
,
after
modalidad
in your query.
Oh thank you so much! i didn't notice
But now i'm getting this: Uncaught Error: Error serializing
.owner
returned from
getServerSideProps
in "/propiedad/[slug]". Reason:
undefined
cannot be serialized as JSON. Please use
null
or omit this value.
At least it’s not a Sanity problem anymore 🙂
I think the document you’re looking at doesn’t have an owner set so you’re assigning undefined to the
owner
prop, which doesn’t work.
If the props map 1:1 with the query response you can do this instead, which should avoid the error:

return {
  props: { ...property }
}
or

owner: property.owner || null,
Nice catch Cory, I totally overlooked that missing comma. Shame on me. 🤦‍♀️
As Cory said, to pass down props from the server to the client, Next uses JSON and undefined cannot be serialized in JSON which is why you need to use null or false or whatever value that can be serialized.

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?