Issue with dereferencing in a Sanity.io project.
5 replies
Last updated: Nov 2, 2021
W
What am I doing wrong here?
This is all im getting
{ 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, } } } }
{ _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
ctaSectionreference because it’s being returned by the
….
Nov 2, 2021, 5:57 PM
W
Good one, this is what I changed it to:
But I'm getting
*[_type == 'insurance' && slug.current == $slug][0]{ ..., pageBuilder[]->{ ..., ctaSection{ picture } } }
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:
Once you get the dereference working, you can build out your ctaSection projection to how you want it.
*[_type == 'insurance' && slug.current == $slug][0]{ ..., pageBuilder[]{ _type == 'content' => @, _type == 'ctaSection' => @-> } }
Nov 2, 2021, 6:24 PM
For an array mixed with references and non-references, I’d recommend an approach like this:
Once you get the dereference working, you can build out your ctaSection projection to how you want it.
*[_type == 'insurance' && slug.current == $slug][0]{ ..., pageBuilder[]{ _type == 'content' => @, _type == 'ctaSection' => @-> } }
Nov 2, 2021, 6:24 PM
W
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.