GROQ query with slug parameter returning null despite matching document

2 replies
Last updated: Jul 28, 2023
Hello I've got a query`export const postsQuery = `*[_type == "blogs"]`;` which returns the following result:

[

{

cover: { _type: 'image', asset: [Object] },

author: 'Abrar  Rauf',

_id: 'd46dcf25-f467-4636-8028-b8a4c1b17001',

body: [ [Object], [Object], [Object], [Object] ],

_updatedAt: '2023-07-27T12:44:54Z',

slug: { current: 'another-blog', _type: 'slug' },

_createdAt: '2023-07-26T11:55:16Z',

_rev: 'cC8oCPRPnURX9jporz2Jb9',

_type: 'blog',

title: 'Another blog'

}

]

I've got a second query `export const postQuery = `*[_type == "blogs" && slug.current == $slug][0]`;` to fetch this individual blog.

const params = {slug: 'another-blog'};

// This is using Sanity client

const post = _await_ getClient().fetch(postQuery, params);

console.log(post);
However this is returning null, is there something I'm doing wrong with the query?
AI Update

Looking at your queries, I can spot the issue! In your first query, you're filtering for _type == "blogs" (plural), but in your result data, the actual _type value is "blog" (singular), not "blogs".

Here's what's happening:

Your first query:

*[_type == "blogs"]

This is returning results, which suggests you might have some documents with _type == "blogs", but the example result you showed has _type: 'blog' (singular).

Your second query:

*[_type == "blogs" && slug.current == $slug][0]

This returns null because there's no document with both _type == "blogs" AND slug.current == "another-blog".

The fix:

Change your second query to match the actual type in your data:

export const postQuery = `*[_type == "blog" && slug.current == $slug][0]`;

Also, you should probably update your first query for consistency:

export const postsQuery = `*[_type == "blog"]`;

The query syntax itself is correct - you're properly using GROQ parameters with the $slug syntax, and passing the parameter correctly. The issue is just the type mismatch between what you're querying for ("blogs") and what actually exists in your data ("blog").

If you're still getting null after this change, double-check:

  1. The exact value of _type in your Sanity Studio schema
  2. That the slug value you're passing ('another-blog') exactly matches what's in the document (it looks correct from your example)
Show original thread
2 replies
Does the query return correct results in the Vision tool?
user F
thanks for mentioning the vision tool I've figured it out. I was fetching data called blogs, when it should be blog.

*[_type == "blog"]
-> Returns all the blogs, and then running
*[_type == "blog" && slug.current == $slug]
fixed the issue for me πŸ™‚

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?