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

Issue with fetching data from an array of references and non-references in a groq query.

3 replies
Last updated: Dec 14, 2023
Hey! I'm trying to create a groq query that fetches data from an array of both references and not references.
This is what I currently have, but the not-references returns null, and if I remove the -> after contentLoggedin[], all the other entries return null!


contentLoggedIn[] -> {
    _type == 'staticSlider' => {
      _type,
      _key,
      textIsCentered,
      textColor,
      header {
        headline,
        body,
        callToAction {
          text,
          link
        }
      },
      items[] -> {
        title,
        filterUrl,
        urlSlug,
        image {
          "asset": {
            "url": asset->url
          }
        },
        description
      }
    },
    _type == 'dynamicSlider' => {
      _type,
      content
    }
}
Basically, the dynamicSlider type returns null...
Dec 13, 2023, 1:33 PM
What does the schema for this look like?
Dec 13, 2023, 5:39 PM
Hey
user M
and thanks for the response πŸ™‚
This is the schema for that specific type:


{
  name: 'contentLoggedIn',
  type: 'array',
  title: 'Content for a logged in user',
  description: 'Create, add and order the content of this landing page.',
  of: [
    {
      type: 'reference',
      title: 'Reusable Static Slider or Banner',
      to: [{ type: 'staticSlider' }, { type: 'banner' }, { type: 'twoTileBanner' }],
    },
    { type: 'dynamicSlider' },
  ],
},
And the dynamic slider is just an object with a simple object called "content" which has title and value, like this:

{
  name: 'dynamicSlider',
  type: 'object',
  title: 'Dynamic Slider',
  fields: [
    {
      name: 'content',
      type: 'string',
      title: 'Dynamic Content',
      description: 'Choose what dynamic content to display.',
      options: {
        list: [
          { title: 'Popular items', value: 'popularItems' },
          { title: 'Favourite items', value: 'favouriteItems' },
          { title: 'Recommended items', value: 'recommendedItems' },
        layout: 'radio',
      },
    },
  ],
}
Whereas the other items in the array of contentLoggedIn are references.
Dec 14, 2023, 12:25 PM
I figured it out πŸ™‚
Dec 14, 2023, 2:20 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?