Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Issue with dereferencing in a Sanity.io project.

5 replies
Last updated: Nov 2, 2021
What am I doing wrong here?
{
      name: "pageBuilder",
      type: "array",
      title: "Page content",
      of: [
        { type: "content" },
        {
          type: "reference",
          name: "ctaSection",
          title: "CTA",
          to: [{ type: "ctaBlocks" }],
        },
      ],
    },

*[_type == 'insurance' && slug.current == $slug][0]{
    ...,
    ctaSection->{
      picture,
      title,
      cta{
        label,
        reference->{
          _type,
          "slug": "/" + slug.current,
        }
      }
    }
  }
This is all im getting

{
      _key: '6569f1186d08',
      _ref: 'c92ccb9a-5441-4fca-8dea-8fb7cbbb7cfd',
      _type: 'ctaSection'
    }
Nov 2, 2021, 5:53 PM
Looks like you also need to specify the array (
pageBuilder
) in your projection. Currently, you’re getting that
ctaSection
reference because it’s being returned by the
.
Nov 2, 2021, 5:57 PM
Good one, this is what I changed it to:
*[_type == 'insurance' && slug.current == $slug][0]{
    ...,
    pageBuilder[]->{
      ...,
      ctaSection{
        picture
      }
    }
  }
But I'm getting

pageBuilder: [
    null,
    {
      _createdAt: '2021-10-09T16:18:59Z',
      _id: 'c92ccb9a-5441-4fca-8dea-8fb7cbbb7cfd',
      _rev: 'Rfna04Xn7gw6L6Geea4aP5',
      _type: 'ctaBlocks',
      _updatedAt: '2021-10-09T19:06:10Z',
      cta: [Object],
      ctaSection: null,
      picture: [Object],
      title: 'title of insurance'
    }
  ],
Nov 2, 2021, 6:04 PM
For an array mixed with references and non-references, I’d recommend an approach like this:

*[_type == 'insurance' && slug.current == $slug][0]{
  ...,
  pageBuilder[]{
    _type == 'content' => @,
    _type == 'ctaSection' => @->
  }
}
Once you get the dereference working, you can build out your ctaSection projection to how you want it.
Nov 2, 2021, 6:24 PM
For an array mixed with references and non-references, I’d recommend an approach like this:

*[_type == 'insurance' && slug.current == $slug][0]{
  ...,
  pageBuilder[]{
    _type == 'content' => @,
    _type == 'ctaSection' => @->
  }
}
Once you get the dereference working, you can build out your ctaSection projection to how you want it.
Nov 2, 2021, 6:24 PM
Yes that works! Thank you
Nov 2, 2021, 6:28 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?