Discussion on targeting data in a query and accessing file URLs in Sanity.io

38 replies
Last updated: Jun 27, 2022
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
*[ _type == "product" && title == "cobber-cb003s" ]
Jun 27, 2022, 7:31 AM
ah yea that works perfectly thanks
Jun 27, 2022, 7:37 AM
I used this
"documentURL": file.asset->url
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
Jun 27, 2022, 7:39 AM
"files": document[].asset->url
Jun 27, 2022, 7:40 AM
hmm this also aint right. Might it have to do with how I build the schema?
Jun 27, 2022, 7:42 AM
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
Oh, what’s the media type?
Jun 27, 2022, 7:43 AM
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
Ah right.
"files": document[].mediafile.asset->url
Jun 27, 2022, 7:46 AM
I think we are getting closer, but it still results in:
Jun 27, 2022, 7:47 AM
You’re missing a comma.
Jun 27, 2022, 7:49 AM
oh right
Jun 27, 2022, 7:50 AM
I still need to target something here it seems
Jun 27, 2022, 7:50 AM
Ah yes, my bad.
"files": document[]->mediafile.asset->url
Jun 27, 2022, 7:52 AM
ah yes this is great now it shows all de urls, Thanks for helping out again !
Jun 27, 2022, 7:54 AM
Of course. 🙂
Jun 27, 2022, 7:55 AM
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
oh this is amazing ! thanks !
Jun 27, 2022, 7:58 AM
just to get this straight because this now has to be different right ?
href={`${sanitydata.document}?dl=`}
we targeting file first at this point
Jun 27, 2022, 7:59 AM
Right, so in your data
files
will be an array of URL. So you’ll need to accommodate for that. With a loop or something. Basically
files
will look like this:
[
  "https://…",
  "https://…",
  "https://…",
  "https://…",
  "https://…"
]
Jun 27, 2022, 8:00 AM
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
href={`${sanitydata[0].files.document}?dl=`}
or this

href={`${sanitydata[0].files}?dl=`}
Jun 27, 2022, 8:04 AM
sanitydata.files[0]
Jun 27, 2022, 8:05 AM
Your whole data is not an array, it’s an object. It’s the
files
value that is.
Jun 27, 2022, 8:06 AM
oh yea ofc I placed it wrong
Jun 27, 2022, 8:07 AM
but doing so it cant read it:
TypeError: Cannot read properties of undefined (reading '0')

Jun 27, 2022, 8:08 AM
That means
files
is undefined, somewhat.
Jun 27, 2022, 8:08 AM
Ah no, wait. Your data is an array, since you get several products.
Jun 27, 2022, 8:08 AM
And then each product in your array has
files
.
Jun 27, 2022, 8:09 AM
I think I got it
href={`${sanitydata[0].files[0]}?dl=`}
Jun 27, 2022, 8:14 AM
yep this works I can confirm hahaha
Jun 27, 2022, 8:14 AM
so the data needs to be defined to [0] and then the files also
Jun 27, 2022, 8:15 AM
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
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
it would look like this instead
*[_type == "product" && title == "cobber-cb003s"][0]{

Jun 27, 2022, 8:18 AM
Right.
Jun 27, 2022, 8:18 AM
It looks also cleaner this way
Jun 27, 2022, 8:19 AM
Thanks again
Jun 27, 2022, 8:19 AM
lets say I want this all in a component and grab it like
<Product/>
Does it need to be a useEffect? or you cant do that with components?
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.

Was this answer helpful?