Difficulty accessing asset properties in a query, but issue was resolved.
Looking at your query, the issue is with how you're dereferencing the asset reference within the nested array projection. When you have an array of objects that contain asset references, you need to use the proper syntax to expand those references.
In your current query:
"assets": project->assets[]{
assetGroup,
"url": asset->url
}The problem is that asset->url is trying to access url directly, but you need to properly dereference the asset first. Based on the GROQ documentation on expanding references, when you have an array of objects containing references, you need to use the dereference operator -> with a projection.
Here's the corrected approach:
const query = `{"data": *[_id == "projectsArray"] {
"projects": projects[]{
"builtAt": project->builtAt,
"description": project->description,
"id": project->_id,
"slug": project->slug,
"title": project->title,
"year": project->year,
"cover": project->cover.asset->{
assetId,
url,
metadata
},
"assets": project->assets[]{
assetGroup,
"assetDetails": asset->{
_id,
url,
metadata
}
}
}
}}`The key changes:
For the cover image: Changed from trying to access individual properties to using a proper projection:
cover.asset->{assetId, url, metadata}For the assets array: Instead of
asset->url, useasset->{_id, url, metadata}to properly expand the entire asset reference and get all the fields you need
The dereference operator -> follows the reference to fetch the actual asset document. When you want multiple fields from that referenced document, you need to use a projection ->{ fields } after the dereference operator. This is explained in the section on expanding references.
If you only want the URL as a simple value in your assets array, you could also use a "naked projection" like this:
"assets": project->assets[]{
assetGroup,
"url": asset->url
}But make sure asset is actually the field name in your schema. If it's named differently (like image or file), adjust accordingly.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.