Sanity query returning array with empty objects instead of articles

9 replies
Last updated: Jul 25, 2023
I’m trying to make a query to get an
n
number of articles from a collection but is returning an array with 3 empty objects.
AI Update

This is a common issue that usually happens when your query is returning documents but your projection isn't correctly accessing the fields you need. Here are the most likely causes and solutions:

Check Your Projection

When you get empty objects back, it typically means your projection is referencing fields that don't exist on those documents. Make sure you're projecting the actual field names in your schema:

// ❌ This might return empty objects if fields don't match
*[_type == "article"][0..n]{
  heading,
  description
}

// ✅ Use the exact field names from your schema
*[_type == "article"][0..n]{
  title,
  body,
  publishedAt
}

Verify Your Document Type

Double-check that your _type filter matches exactly what's in your dataset. Type names are case-sensitive:

// Make sure this matches your schema
*[_type == "article"]

You can verify this by running a simpler query first:

// See what fields are actually available
*[_type == "article"][0]

Common Field Access Issues

If you're trying to access nested fields or references, make sure you're using the correct syntax:

*[_type == "article"][0..n]{
  title,
  "authorName": author->name,  // Use -> to dereference references
  "categoryTitle": category->title
}

Check Your Data

Sometimes the documents exist but don't have the fields populated yet. You can inspect what's actually in your documents using Sanity's Vision plugin in your Studio, or by querying without a projection:

*[_type == "article"][0..3]

This will show you the raw document structure so you can see exactly what fields are available.

Slice Syntax

Make sure you're using the correct slice syntax for limiting results. According to the GROQ pipeline components documentation, you should use two dots (..) for inclusive slicing:

*[_type == "article"][0..9]  // Gets first 10 articles (indices 0-9, both inclusive)

If you want to exclude the right-hand index, use three dots (...):

*[_type == "article"][0...10]  // Gets first 10 articles (indices 0-9, excludes index 10)

Note that ranges are zero-based. With two dots, both indices are included; with three dots, the right index is excluded.

Most Likely Solution

The most common fix is ensuring your projection field names match exactly what's in your schema. Try running this diagnostic query first:

*[_type == "article"][0..2]

This will show you the full document structure without any projection, so you can see all available fields. Then adjust your projection to match the actual field names.

If you share your actual query and schema structure, the community can help pinpoint the exact issue!

Show original thread
9 replies
Initially I had the following query which returned an object with a property
articles
and the array inside:

const ARTICLE_BY_COLLECTION_QUERY = groq`
  *[_type == "articleCollection" && slug.current == $collection][0]{
  "articles": articles[0...$n]->{
    ${ARTICLE_FIELDS}
  }
}
`;

However I wanted to have an array returned from the articles so I tried doing this:

const ARTICLE_BY_COLLECTION_QUERY = groq`
  *[_type == "articleCollection" && slug.current == $collection][].articles[0...$n]->{
    _id
  }
`;
the query works as expected on the sanity vision but when making the actual query on the vue client it returns an empty array.
Any ideas why?
What version of the API are you using?
nvm somehow it’s working now 😅
thanks anyways!
I love when something decides to fix itself!
same!

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?