πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Issue with groq query not returning desired result on front end

10 replies
Last updated: Mar 22, 2024
Hi, seem to be having an issue with a groq query I'm using on my site.
I have an "seoImage" schema, which just contains an alt string field, and the built in image field.

Across my entire site, I use this groq query:


seoImage {
       ...,
       image {
         asset-> {
           ...,
           metadata
         }
       }
     }
However, for one image, I don't get anything back from the result. I just get the asset reference back with a _type and _ref properties.

I've tried doing the exact query I'm running in the vision plugin inside sanity, and I get the desired result (The image data, along with the metadata of the image).

Why isn't this working on my front end? but is working for all my other images?
Mar 18, 2024, 1:13 PM
To be clear, your document has:
"seoImage": {
  "image": {
    "asset": {
      "_ref": "..."
      "_type": "reference"
    }
  }
}
and
seoImage.image.asset->
isn't working?
Mar 18, 2024, 3:50 PM
Hi User, yes that was my issue.
I did however figure out what was causing this to not follow the reference:

my entire query was the following:


*[_type == 'product' && _id == $_id][0] {
			intro {
				title,
				content,
				seoImage {
					...,
					image {
						...,
						asset-> {
							...,
							metadata
						}
					}
				},
				button {
					...,
					link {
						type,
						type == "internal" => internalLink {
							reference-> {
								_type,
								"slug": slug.current,
							}
						},
						type == "external" => externalLink {
							url,
						},
					}
				},
                ...,
			},
		}
It was the last spread operator inside the intro object that was causing the issue. Removing this allows the full image data to come through.

I noticed it wasn't an issue with the image when I added in my button, and also found it wasn't following the internalUrl references. Again, this was fixed upon removed the final spread operator.
Mar 18, 2024, 3:54 PM
Ah yeah, the spread operator will overwrite.
Mar 18, 2024, 4:04 PM
If I'd put that spread operator right at the beginning, am I correct in assuming that wouldn't have caused an issue?
Mar 18, 2024, 4:09 PM
Yes
Mar 18, 2024, 4:14 PM
Awesome, thanks for clearing that up for me, sorry for wasting your time!!
Mar 18, 2024, 4:23 PM
No worries!
Mar 18, 2024, 6:40 PM
I'd like to do this exactly, but dynamically. Hardcoding in every instance where an image reference "could" be seems silly to me. Is doing this dynamically possible?
Mar 21, 2024, 8:42 PM
Not at the moment, sorry.
Mar 22, 2024, 10:29 AM
This is something we know is a problem, and we are actively working on possible solutions.
Mar 22, 2024, 10:29 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?