Issue with referenced array returning null in a query using the -> operator.
9 replies
Last updated: Apr 13, 2022
D
Hi, does anyone have any ideas why my referenced array might be returning null when I use the -> operator? Here’s my query:
Gallery is type i have created that has a galleryImageReference which cycles through galleryImage. I have five images in the gallery but it gets five entries which are all null. The categories dereferencer works fine.
*[_type == "page" && title == "Home"]{ ..., gallery { images[]-> }, "mainImageUrl": mainImage { asset->{url} }, categories[]-> }
Apr 13, 2022, 4:07 PM
D
Here’s the full result:
"result":[1 item 0:{14 items "_createdAt":"2022-04-05T16:12:44Z" "_id":"723debd8-3524-4957-a07f-2fe2fda47259" "_rev":"BarLwW8hBDpE0bK0rKdNF9" "_type":"page" "_updatedAt":"2022-04-13T15:10:40Z" "author":{2 items "_ref":"0d136d25-12ea-4f58-b594-78f7e330cee3" "_type":"reference" } "body":[1 item 0:{...}5 items ] "categories":[2 items 0:{...}7 items 1:{...}7 items ] "gallery":{1 item "images":[5 items 0:NULL 1:NULL 2:NULL 3:NULL 4:NULL ] } "mainImage":{2 items "_type":"image" "asset":{...}2 items } "mainImageUrl":{1 item "asset":{...}1 item } "publishedAt":"2022-04-05T16:11:55.349Z" "slug":{2 items "_type":"slug" "current":"home" } "title":"Home" } ]
Apr 13, 2022, 4:08 PM
M
user P
What do the objects inside the images array look like? You may need a query similar to the MainImageUrl if the actual image ref/asset is inside another objectApr 13, 2022, 4:43 PM
D
Hi User,Here ’s some code.
gallery.js is the top level
galleryImageReference is:
and that’s referencing:
gallery.js is the top level
export default { name: 'gallery', title: 'Gallery', type: 'document', fields: [ { name: 'title', title: 'Title', type: 'string', }, { name: 'images', title: 'Images', type: 'array', description: 'It is good to have images in a gallery', of: [ { name: 'galleryImage', type: 'galleryImageReference', title: 'Gallery Image' } ] }, ] }
export default { name: 'galleryImageReference', type: 'object', title: 'Gallery Image reference', fields: [ { type: 'reference', name: 'GalleryImage', to: [ { type: 'galleryImage', } ] } ], preview: { select: { title: 'galleryImage.title', media: 'galleryImage.image.asset', } } }
export default { name: 'galleryImage', title: 'Gallery Image', type: 'document', fields: [ { name: 'title', title: 'Title', type: 'string', }, { name: 'image', title: 'Picture', type: 'image', }, { name: 'alt', title: 'Alt', type: 'string', }, { name: 'caption', title: 'Caption', type: 'string', }, ] }
Apr 13, 2022, 5:22 PM
D
I can’t get the previews to work either for some reason.
Apr 13, 2022, 5:23 PM
M
user P
I’m struggling to understand the hierarchy, but it might begallery { images[]->{ ..., image { ..., asset-> } },
Apr 13, 2022, 5:38 PM
D
Tbh I’m beginning to think I need to go back and do a load of reading of the docs to get a better handle on it.I’ve tried a few variations similar to this already and it seems to break at the images[] point. I think the fact that the preview doesn’t work either is probably related and I need to pick through some basics and go on from there.
Thanks for your help!
Thanks for your help!
Apr 13, 2022, 5:49 PM
M
user P
I find that the document inspector in the studio is very helpful for understanding the structure of the data.Apr 13, 2022, 6:17 PM
M
This shows you the output data object that you are querying against, and might help you find where the reference is happening
Apr 13, 2022, 6:18 PM
D
That inspector was very helpful Thanks User! I’ve just tried this after taking a look at that:
and it’s given me most of what I want to see.
Still going to spend a while reading the docs properly though. :)
gallery { images[]{ GalleryImage->{ ..., asset->{url} } }, },
Still going to spend a while reading the docs properly though. :)
Apr 13, 2022, 9:18 PM
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.