GROQ query not returning expected results on frontend, but works in Vision
6 replies
Last updated: Oct 28, 2022
B
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*:
Frontend (Nuxt 3, Nuxt Sanity):
Troubleshooting:• The proper documents are returned if I delete the string key and enclosing brackets. Full Query:
• The proper documents are returned if I delete the “count” part:
While I’m typing this I got the feeling that this might be an error in the Nuxt Sanity Module I’m using.
*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)]
{ 'countries': *[_type == 'country'], 'authorities': *[_type == 'network' && type == 'authority']|order(title, asc), 'partners': *[_type == 'network' && type == 'partner']|order(title, asc) }
Oct 24, 2022, 6:09 PM
B
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
But the next two work just fine:
No object, no attribute but
the same query inside another anrelated query:
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
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)] }
No object, no attribute but
count()
*[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)]
*[_type == 'network']{ 'countries': *[_type == 'country' && (count(*[_type=='network' && references(^._id)]) > 0)] }
Oct 25, 2022, 7:07 AM
B
Updating the API version fixed this.
Oct 25, 2022, 10:28 AM
B
I didn’t specify a Version in the js client, so that would be v1 I guess?
Oct 28, 2022, 9:55 AM
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.