Issue with querying references in Sanity v3.47 resolved
28 replies
Last updated: Jun 20, 2024
I
Hello. I am having issues with querying references. I keep getting errors in parse response: I get response if query does not include references and weirdly the reference semi works when there is just one item not array of references. Anyone experiencing similar issues? I have just upgraded to v3.47.
Unexpected non-white character after JSON.
Jun 20, 2024, 2:33 PM
N
Hey User! Can you send us over the query that is causing the issue?
Jun 20, 2024, 2:38 PM
I
*[_type == "platformHome"][0]{
_id,
title,
headline,
subheadline,
cards[]{
_key,
title,
subtitle,
platformLink->{"slug": slug.current}
}
}
I am trying to reference link to the page so I query said pages and extract just a slug. It was working before. I had the same issue with image url but it seems to be working now
Jun 20, 2024, 2:57 PM
N
If you remove all the fields like _id, title, etc and slowly add them back, which one causes the error?
Jun 20, 2024, 3:00 PM
I
I get response in studio. I use NextJS
Jun 20, 2024, 3:00 PM
N
At first glance that query looks right
Jun 20, 2024, 3:00 PM
I
I did it. Only platform link causes the problem
Jun 20, 2024, 3:00 PM
N
OH WAIT
Jun 20, 2024, 3:01 PM
N
I SEE
Jun 20, 2024, 3:01 PM
N
let me test something quick
Jun 20, 2024, 3:01 PM
I
When I do platformLink-> only, o don’t get the error. But once I query fields in that document it breaks
Jun 20, 2024, 3:01 PM
I
Just tested other fields. Only slug causes the problem.
Jun 20, 2024, 3:03 PM
N
platformLink->{slug}
does this work?
Jun 20, 2024, 3:05 PM
I
platformLink->{slug} is working
Jun 20, 2024, 3:05 PM
N
Ah okay heres why:
Jun 20, 2024, 3:05 PM
I
Haha. Just came upWith the same
Jun 20, 2024, 3:05 PM
N
so not working?
Jun 20, 2024, 3:05 PM
I
It’s working
Jun 20, 2024, 3:06 PM
I
Thanks! I used the other solution before and it was working so not sure what changed.
Jun 20, 2024, 3:06 PM
N
platformLink->{slug}
^^ This works because
^^ This works because
slugis a property of the object already,
slug: slug.currentisnt possible because its a circular dependancy. In Javascript you cant assign something to itself using its own value
Jun 20, 2024, 3:06 PM
N
No problem! If you did something like
url: slug.currentthat would work
Jun 20, 2024, 3:07 PM
N
Assuming no other field is named url
Jun 20, 2024, 3:07 PM
I
I found
“slug”: slug.currentin documentation hence used it before. Good to know different approaches.
Jun 20, 2024, 3:08 PM
N
Really? That surprising that works (and tbh I might be completely wrong but in my brain it makes sense) lol
Jun 20, 2024, 3:09 PM
N
Can you share where thats from?
Jun 20, 2024, 3:09 PM
I
I’ll look it up. Can’t remember from top of my head.
Jun 20, 2024, 3:11 PM
N
Thank you!
Jun 20, 2024, 3:14 PM
I
But if you test it in studio you get correct response hence I never questioned it.
Jun 20, 2024, 3:15 PM
N
Hmmm thats wild, good chance my reasoning is wrong if it works inside of vision
Jun 20, 2024, 3:16 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.