✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Issue with fetching individual blog using query in Sanity.io

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?
Jul 28, 2023, 9:11 AM
Does the query return correct results in the Vision tool?
Jul 28, 2023, 10:11 AM
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 🙂
Jul 28, 2023, 12:46 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?