Querying image URLs in Sanity.io schema and data
22 replies
Last updated: May 26, 2023
S
Hi all,
I have an object as such:
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:
What would be the correct way to fetch the URLs ?
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", }, ], }, ], })
My current query:
logo_set[]{ 'logos': brand_logo.asset->url }
May 25, 2023, 10:19 AM
A
What does this return?
logo_set[]{ 'logos': brand_logo.asset }
May 25, 2023, 10:54 AM
S
its returns:
logo_set:[…] 2 items 0:{…} 1 property logos:null 1:{…} 1 property logos:null
May 25, 2023, 10:58 AM
A
Sounds like there is no asset. What does this return:
logo_set[]{ 'logos': brand_logo }
May 25, 2023, 11:10 AM
S
same…
May 25, 2023, 11:11 AM
A
Heh, ok, so what do you get with just:
logo_set
May 25, 2023, 11:11 AM
A
Also, what does your entire query look like?
May 25, 2023, 11:11 AM
S
My entire query looks like this:
And the result of the above is:
*[_type == "page"]{ page_content[_type == "logo_row_section"]{ logo_set[] } }
[…] 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
A
There's nothing called
brand_logohere
May 25, 2023, 11:26 AM
A
You could do
logo_set[] { asset->url }
May 25, 2023, 11:26 AM
S
Sorry my bad… I just changed the code, to try another apporach
Will try this:
Will try this:
logo_set[] { asset->url }
May 25, 2023, 11:27 AM
S
It returns:
Attribute or a string key expected
May 25, 2023, 11:27 AM
A
Sorry, just add
"url":in front
May 25, 2023, 11:27 AM
A
logo_set[] { "url": asset->url }
May 25, 2023, 11:28 AM
S
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 ?
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
A
You mean why the
"url"part?
May 25, 2023, 11:31 AM
A
There is no property called
brand_logo, so you cannot select it
May 25, 2023, 11:31 AM
S
Actually the array now looks like this:
Hence there’s no
But this query:
What I wanted to know is, why doesn’t this work:
defineField({ name: "logo_set", title: "Logo Set", type: "array", of: [ { name: "logo", title: "Logo", type: "image", fields: [ { name: "alt", title: "Alternative text", type: "string", }, ], }, ], }),
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
A
Well, it looks like your data doesn't match your schema
May 25, 2023, 11:43 AM
A
If your data followed your schema, it would be
logo.asset, but there's no
logoin your current array
May 25, 2023, 11:43 AM
S
the “image” type has the name and title of “logo”
May 25, 2023, 11:48 AM
A
Not in your current data
May 26, 2023, 9:53 AM
A
ofis an array of objects, and
logoisn'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.