Sanity client.fetch() returns empty array despite documents existing in dataset

4 replies
Last updated: Dec 2, 2021
Hi everyone! happy friday :party_parrot:
I'm trying to use
@sanity/client
to fetch some data from the CMS but something's not working since I keep getting an empty array when executing
client.fetch("*[_type == 'document']")
(my dataset already has some documents created as I've tested in Vision) .
Does anybody know how to debug this? I've no clue on what's going on since there's no error but a response with an empty array; I'm setting up the client with the right
projectId
and
dataset
but I can't get any data 😢 I've changed the dataset to be public but still no luck
AI Update

I feel your pain! This is actually a common issue that trips people up, and it's likely related to the perspective setting in your client configuration.

The most common reason you're getting an empty array is that your Sanity client is querying with the published perspective (which is now the default as of API version v2025-02-19), but your documents might only exist as drafts in the dataset. When you create documents in Sanity Studio, they start as drafts (with _id starting with drafts.) and only become "published" documents when you explicitly publish them.

Here's how to debug and fix this:

1. Check if your documents are published

In Sanity Studio, look at your documents and make sure they have a "Publish" button that's been clicked. If they're still drafts, they won't show up in queries using the published perspective.

2. Adjust your client configuration

If you want to see draft documents, you need to configure your client with the previewDrafts perspective or use a token for authentication. Here's how:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  useCdn: false, // Important: set to false when querying drafts
  apiVersion: '2024-01-01', // use a date-based version
  perspective: 'previewDrafts', // This will return drafts with fallback to published
  token: 'your-read-token' // Required for previewDrafts perspective
})

Key things to note:

  • useCdn: false - The CDN only serves published content, so you need to disable it to see drafts
  • perspective: 'previewDrafts' - This perspective returns draft versions when they exist, falling back to published versions
  • token - You need an authenticated token (with read permissions) to access draft content. You can create one in your Sanity project's API settings

3. Alternative: Query for drafts explicitly

If you just want to see what's actually in your dataset, you can query for drafts specifically:

const drafts = await client.fetch("*[_type == 'document' && _id in path('drafts.**')]")
const published = await client.fetch("*[_type == 'document' && !(_id in path('drafts.**'))]")

4. Quick debug query

Try this query in your code to see what's actually there:

const allDocs = await client.fetch("*[_type == 'document'] { _id, _type, _updatedAt }")
console.log('Found documents:', allDocs)

If this returns empty but Vision shows results, it confirms it's a perspective/CDN issue.

The API versioning changes introduced a shift in the default perspective from raw to published, which is likely what's catching you here. Good luck, and happy Friday! 🎉

Show original thread
4 replies
I would double check that the dataset is set to public – especially if it used to be private. This would be consistent with the behaviour. Also: do you have documents that are type
document
? What do you get for just
*
?
Expanding on Even's suggestion to check your document types, it might help to know that the
type: 'document'
in your schema definitions is not the
_type
you query later. The
_type
is the
name
you set in your schema definitions, e.g.
name: 'article'
. Sorry about the confusion there 🙂
Hey
user M
user A
; sorry for the late reply; I was able to solve the issue with Even suggestion. As Peter described I was retrieving with 'document' type; thanks for the help 🙌
Great to hear!

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?