Discussion on targeting data in a query and accessing file URLs in Sanity.io
38 replies
Last updated: Jun 27, 2022
S
Hi everyone,How do I target cobber-cb003s in the query so that it only gives me that data in the list?
Jun 27, 2022, 7:30 AM
K
*[ _type == "product" && title == "cobber-cb003s" ]
Jun 27, 2022, 7:31 AM
S
ah yea that works perfectly thanks
Jun 27, 2022, 7:37 AM
S
I used this
before to target document and have it downloadable on the front end. I have now 4 other documents in this query like this , but doing it the old way returns me null
"documentURL": file.asset->url
Jun 27, 2022, 7:39 AM
K
"files": document[].asset->url
Jun 27, 2022, 7:40 AM
S
hmm this also aint right. Might it have to do with how I build the schema?
Jun 27, 2022, 7:42 AM
S
Schema looks like this
export default { name: "product", title: "Product", type: "document", ["update", "create", "publish","delete"], fields: [ { title: "Title", description: "This title will appear in the title unit at the top of the page", name: "title", type: "string", }, { title: 'Text', name: 'text', type: 'array', of: [{type: 'block'}] }, { title: 'Model3D URL', name: 'imageUrl', type: 'url' }, { title: 'Document', name: 'document', type: 'array', of: [ { type: "reference", to: [{type: "media"}] } ] }, ], };
Jun 27, 2022, 7:42 AM
K
Oh, what’s the media type?
Jun 27, 2022, 7:43 AM
S
that reverse to the other schema that contains this:
export default { name: "media", title: "Media", type: "document", ["update", "create", "publish", "delete"], fields: [ { title: "Name", description: "Name your file", type: "string", name: "name", }, { title: 'Media file', description: "Place media files here", name: 'mediafile', type: 'file', fields: [ { name: 'description', type: 'string', title: 'Description' } ] }, ], };
Jun 27, 2022, 7:44 AM
K
Ah right.
"files": document[].mediafile.asset->url
Jun 27, 2022, 7:46 AM
S
I think we are getting closer, but it still results in:
Jun 27, 2022, 7:47 AM
K
You’re missing a comma.
Jun 27, 2022, 7:49 AM
S
oh right
Jun 27, 2022, 7:50 AM
S
I still need to target something here it seems
Jun 27, 2022, 7:50 AM
K
Ah yes, my bad.
"files": document[]->mediafile.asset->url
Jun 27, 2022, 7:52 AM
S
ah yes this is great now it shows all de urls, Thanks for helping out again !
Jun 27, 2022, 7:54 AM
K
Of course. 🙂
Jun 27, 2022, 7:55 AM
K
Breakdown so you get what’s going on:
"files": // Declare a property that will hold our data document[] // The `document` property is an array, hence the brackets (kinda like a loop) -> // It’s an array of references, so we need to follow them mediafile // Then we want the content of the `mediafile` property of each media .asset // Which is a `file` type, so it has an `asset` property -> // Which needs to be resolved as usual url // And finally we can get the url
Jun 27, 2022, 7:57 AM
S
oh this is amazing ! thanks !
Jun 27, 2022, 7:58 AM
S
just to get this straight because this now has to be different right ?
we targeting file first at this point
href={`${sanitydata.document}?dl=`}
Jun 27, 2022, 7:59 AM
K
Right, so in your data
fileswill be an array of URL. So you’ll need to accommodate for that. With a loop or something. Basically
fileswill look like this:
[ "https://…", "https://…", "https://…", "https://…", "https://…" ]
Jun 27, 2022, 8:00 AM
S
yea so I target it with [0] for the first in the array but the naming confention should be different now. so either I'm close to something like this
or this
href={`${sanitydata[0].files.document}?dl=`}
href={`${sanitydata[0].files}?dl=`}
Jun 27, 2022, 8:04 AM
K
sanitydata.files[0]
Jun 27, 2022, 8:05 AM
K
Your whole data is not an array, it’s an object. It’s the
filesvalue that is.
Jun 27, 2022, 8:06 AM
S
oh yea ofc I placed it wrong
Jun 27, 2022, 8:07 AM
S
but doing so it cant read it:
TypeError: Cannot read properties of undefined (reading '0')
Jun 27, 2022, 8:08 AM
K
That means
filesis undefined, somewhat.
Jun 27, 2022, 8:08 AM
K
Ah no, wait. Your data is an array, since you get several products.
Jun 27, 2022, 8:08 AM
K
And then each product in your array has
files.
Jun 27, 2022, 8:09 AM
S
I think I got it
href={`${sanitydata[0].files[0]}?dl=`}
Jun 27, 2022, 8:14 AM
S
yep this works I can confirm hahaha
Jun 27, 2022, 8:14 AM
S
so the data needs to be defined to [0] and then the files also
Jun 27, 2022, 8:15 AM
K
Well, that depends.
[0]means the first one in the array. If you query an array, that means you probably need all its content. So
[0]might not be enough and a loop is probably expected.
Jun 27, 2022, 8:16 AM
S
Oh yeah I can change that in the query so that I dont have to define the first of array in the ref
Jun 27, 2022, 8:18 AM
S
it would look like this instead
*[_type == "product" && title == "cobber-cb003s"][0]{
Jun 27, 2022, 8:18 AM
K
Right.
Jun 27, 2022, 8:18 AM
S
It looks also cleaner this way
Jun 27, 2022, 8:19 AM
S
Thanks again
Jun 27, 2022, 8:19 AM
S
lets say I want this all in a component and grab it like
Does it need to be a useEffect? or you cant do that with components?
<Product/>
Jun 27, 2022, 8:27 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.