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

Issue with referenced array returning null in a query using the -> operator.

9 replies
Last updated: Apr 13, 2022
Hi, does anyone have any ideas why my referenced array might be returning null when I use the -> operator? Here’s my query:
*[_type == "page" && title == "Home"]{
      ..., 
      gallery { 
       images[]->
      },
      "mainImageUrl": mainImage { asset->{url} }, 
      categories[]->
    }
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.
Apr 13, 2022, 4:07 PM
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
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 object
Apr 13, 2022, 4:43 PM
Hi User,Here ’s some code.
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'
                }
            ]
        },
    ]
}
galleryImageReference is:

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',
    }
  }
}
and that’s referencing:

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
I can’t get the previews to work either for some reason.
Apr 13, 2022, 5:23 PM
user P
I’m struggling to understand the hierarchy, but it might be

gallery { 
   images[]->{ ..., image { ..., asset-> }
},
because the galleryImage type has a field of image inside it which would hold the reference
Apr 13, 2022, 5:38 PM
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!
Apr 13, 2022, 5:49 PM
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
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
That inspector was very helpful Thanks User! I’ve just tried this after taking a look at that:
gallery { 
         images[]{ GalleryImage->{ ..., asset->{url} } },
      }, 
and it’s given me most of what I want to see.
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.

Was this answer helpful?