Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Querying image URLs in Sanity.io schema and data

22 replies
Last updated: May 26, 2023
Hi all,
I have an object as such:

defineField({
      name: "logo_set",
      title: "Logo Set",
      type: "array",
      of: [
        {
          name: "brand_logo",
          title: "Brand Logo",
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alternative text",
              type: "string",
            },
          ],
        },
      ],
    })
I’m trying to query the URLs of the images that will be uploaded, but I’m getting “null” as the value.

My current query:

logo_set[]{
    'logos': brand_logo.asset->url
}
What would be the correct way to fetch the URLs ?
May 25, 2023, 10:19 AM
What does this return?
logo_set[]{
  'logos': brand_logo.asset
}
May 25, 2023, 10:54 AM
its returns:
logo_set:[…] 2 items
  0:{…} 1 property
    logos:null
  1:{…} 1 property
    logos:null
May 25, 2023, 10:58 AM
Sounds like there is no asset. What does this return:
logo_set[]{
  'logos': brand_logo
}
May 25, 2023, 11:10 AM
same…
May 25, 2023, 11:11 AM
Heh, ok, so what do you get with just:
logo_set
May 25, 2023, 11:11 AM
Also, what does your entire query look like?
May 25, 2023, 11:11 AM
My entire query looks like this:
*[_type == "page"]{
        page_content[_type == "logo_row_section"]{
            logo_set[]
        }
    }
And the result of the above is:

[…] 1 item
 0:{…} 1 property
  page_content:[…] 1 item
   0:{…} 1 property
    logo_set:[…] 2 items
      0:{…} 4 properties
        _type:logo
        alt:Random 1
        _key:941bc656cfd4
        asset:{…} 2 properties
          _ref:image-e60765243a3f97511bffe0857bd962b4ad1a3196-380x250-png
          _type:reference
      1:{…} 4 properties
        _key:c16e65039613
        asset:{…} 2 properties
           _ref:image-36f944af4f008d94241194e04d320755ffcd6cd2-380x250-png
           _type:reference
        _type:logo
        alt:Random 2
May 25, 2023, 11:17 AM
There's nothing called
brand_logo
here
May 25, 2023, 11:26 AM
You could do
logo_set[] { asset->url }
May 25, 2023, 11:26 AM
Sorry my bad… I just changed the code, to try another apporach
Will try this:
logo_set[] { asset->url }
May 25, 2023, 11:27 AM
It returns:
Attribute or a string key expected
May 25, 2023, 11:27 AM
Sorry, just add
"url":
in front
May 25, 2023, 11:27 AM
logo_set[] { "url": asset->url }
May 25, 2023, 11:28 AM
Ah, yes! it works now !!!
Thanks a ton!

Can you give me some insight, as to why this query needs to be structured as such ?
May 25, 2023, 11:29 AM
You mean why the
"url"
part?
May 25, 2023, 11:31 AM
There is no property called
brand_logo
, so you cannot select it
May 25, 2023, 11:31 AM
Actually the array now looks like this:
defineField({
      name: "logo_set",
      title: "Logo Set",
      type: "array",
      of: [
        {
          name: "logo",
          title: "Logo",
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alternative text",
              type: "string",
            },
          ],
        },
      ],
    }),
Hence there’s no
brand_logo

But this query:
logo_set[] { "url": asset->url }
worked
What I wanted to know is, why doesn’t this work:
logo_set[] { "url": logo.asset->url }
May 25, 2023, 11:37 AM
Well, it looks like your data doesn't match your schema
May 25, 2023, 11:43 AM
If your data followed your schema, it would be
logo.asset
, but there's no
logo
in your current array
May 25, 2023, 11:43 AM
the “image” type has the name and title of “logo”
May 25, 2023, 11:48 AM
Not in your current data
May 26, 2023, 9:53 AM
of
is an array of objects, and
logo
isn't a field name on each object. I think you need to read more about how schemas work.
May 26, 2023, 9:54 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?