Error querying blog post by slug in Next.js project

6 replies
Last updated: Jun 27, 2023
Hello folks, I’m querying blog posts data from sanity in my nextjs project, I was successful at that, but I’m having error trying to query a blog post by slug. I will appreciate a second eye to look at what I might be doing wrong. Thanks
 error node_modules/@sanity/client/dist/index.cjs (90:12) @ onResponse
- error Error: unable to parse entire expression
    at Object.onceWrapper (node:events:641:28)
    at DestroyableTransform.emit (node:events:539:35)
This is how I’m querying it

export const getSinglePost = async (
  slug: string
): Promise<PostData["data"]> => {
  const client = createClient(clientConfig);

  return await client.fetch(
  *[_type == "post" && slug.current == ${slug}][0] {

const BlogDetails = async ({ params: { slug } }: Params) => {
  const singlePost = await getSinglePost(slug);
  return <BlogDetailsComponent postDetails={singlePost} />;
export default BlogDetails;
Jun 27, 2023, 5:19 PM
user E
. I think it may be that comma after your projection:

  },   // <-- Try removing the comma
Jun 27, 2023, 5:49 PM
user A
thanks this works but my data in null, it does not show
Jun 27, 2023, 6:25 PM
Since you want the slug
, it looks like you’ll want to change
in this line:

*[_type == "post" && slug.current == ${slug}][0] {

Jun 27, 2023, 6:55 PM
It works now, many thanks
user A
. I really appreciate it. It looks like I have to learn the groq query language :simple_smile:
Jun 27, 2023, 9:57 PM
Great! Glad to hear it.
Here are a few resources, if you’re interested:
Jun 27, 2023, 10:02 PM
Thank you
Jun 27, 2023, 11:07 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in