Unlock seamless workflows and faster delivery with our latest releases – get the details

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(
    groq`
  *[_type == "post" && slug.current == ${slug}][0] {
      title,
      "slug":slug.current,
      "author":author->name,
      "image":image.asset->url,
      description,
      category[]->,
      toc[]->,
      body,
      publishedAt
  },
  `,
    {
      slug,
    }
  );
};

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

      publishedAt
  },   // <-- Try removing the comma
  `,
    {
      slug,
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
param
, it looks like you’ll want to change
${slug}
to
$slug
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:


https://hdoro.dev/learn-groq
https://www.sanity.io/docs/how-queries-work
https://www.sanity.io/docs/query-cheat-sheet
Jun 27, 2023, 10:02 PM
Thank you
Jun 27, 2023, 11:07 PM

Sanity– build remarkable experiences at scale

Sanity is a modern 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?