👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

GROQ query not returning expected results on frontend, but works in Vision

6 replies
Last updated: Oct 28, 2022
Hi, I’m having problems with my GROQ query result:
*Problem*:
the “countries” part of my query is ignored on the front and doesn’t return any documents. All “authorities” and “partners” documents are returned correctly. I don’t get an error message on fetching the query on my frontend (like “bad request” or any api/permission related errors).
In Vision it works as expected and all documents that are referenced by a ‘network’ document are returned.

*Query*:

{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)],
  'authorities': *[_type == 'network' && subtype == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && subtype == 'partner']|order(title, asc)
}

Frontend (Nuxt 3, Nuxt Sanity):
const query = groq`{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)],
  'authorities': *[_type == 'network' && subtype == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && subtype == 'partner']|order(title, asc)
}`
const { data, refresh } = useSanityQuery(query, { lang: locale.value })

Troubleshooting:• The proper documents are returned if I delete the string key and enclosing brackets. Full Query:
*[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
• The proper documents are returned if I delete the “count” part:

{
  'countries': *[_type == 'country'],
  'authorities': *[_type == 'network' && type == 'authority']|order(title, asc),
  'partners': *[_type == 'network' && type == 'partner']|order(title, asc)
}
While I’m typing this I got the feeling that this might be an error in the Nuxt Sanity Module I’m using.
Oct 24, 2022, 6:09 PM
Are the network documents referencing the country published?
Oct 24, 2022, 6:43 PM
Yes, and the weird thing is, the exact same query works just fine when used in vision.
after some testing it got even weirder:

This doesn’t work (attribute inside object and
count()
):
{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
}
But the next two work just fine:
No object, no attribute but
count()

*[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
the same query inside another anrelated query:

*[_type == 'network']{
  'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
}
ALL queries work in vision so there seems to be no mistake. I’m 90% convinced it’s the frontend tools I’m using. I’ll create an issue on the github page
Oct 25, 2022, 7:07 AM
Updating the API version fixed this.
Oct 25, 2022, 10:28 AM
Oh great! What version were you previously on?
Oct 25, 2022, 3:25 PM
I didn’t specify a Version in the js client, so that would be v1 I guess?
Oct 28, 2022, 9:55 AM
Ah, got it. It would default to v1 in that case.
Oct 28, 2022, 5:37 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?