Trouble shooting empty array response from groq queries in app development

4 replies
Last updated: Apr 17, 2020
I built my groq queries in vision and I am getting the data I need. But when I put them into my app, I am getting back an empty array. No error messages.
This is in development and I did add localhost to the CORS part of the dashboard.

How can I trouble shoot this further?
AI Update

This is a very common issue! The most likely culprit is the API perspective your client is using. Vision and your app may be querying different perspectives by default, which explains why Vision shows data but your app returns empty arrays.

The Root Cause: Perspective Differences

Sanity Vision typically uses the raw perspective by default, which returns all documents including drafts. However, as of API version 2025-02-19, the default perspective for API clients changed to published, which only returns published documents (not drafts).

If you're working with draft documents in Vision but your app client is using the published perspective, you'll get an empty array because those drafts aren't "published" yet.

How to Fix It

When initializing your Sanity client in your app, explicitly set the perspective to match what you need:

import { createClient } from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  useCdn: false, // Use false for development to see latest changes
  apiVersion: '2025-02-19',
  perspective: 'previewDrafts' // or 'raw' to match Vision's behavior
})

For development/preview environments, use perspective: 'previewDrafts' which will prioritize draft documents over published ones. For production, use perspective: 'published' to only show published content.

Other Troubleshooting Steps

  1. Check if documents are published: In Sanity Studio, make sure you've clicked "Publish" on your documents. Draft documents have IDs prefixed with drafts. - if all your content is drafts and you're querying with the published perspective, you'll get nothing back.

  2. Disable CDN in development: Set useCdn: false in your client config during development. The CDN can cache results and won't reflect immediate changes.

  3. Verify your dataset: Make sure your client is pointing to the same dataset you're using in Vision (usually production or development).

  4. Check API version: If you're using an older API version, the default perspective might be different. Explicitly setting it avoids confusion.

  5. Console log the full client query: Add debugging to see exactly what's being sent:

const result = await client.fetch(query, params)
console.log('Query:', query)
console.log('Params:', params)
console.log('Result:', result)

The perspective mismatch is by far the most common reason for this specific issue - Vision showing data but your app returning empty arrays with no errors. Setting perspective: 'previewDrafts' should get you back on track!

Sounds like you have a private dataset?
Ahhh... I do. I have a token for this set... I'm assuming I can put that into the client constructor
Thanks... found it in the docs too! Apprciate the quick help!
Just be careful with that token :)

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?