🎤 Builder Talk: The Story Behind Lady Gaga’s Digital Experience – Register now

Issue with fetching the name of a referenced tag in a recipe array in React.

5 replies
Last updated: Feb 24, 2021
Ok, this one is complicated. In the content I have "Recipe" with an array of "fresh" ingredients (objects) where the name is a reference to a "Tag" (called: ingredienttag). When I fetch the recipes in React, like:sanityClient
.fetch(

*[_type == "recipe"]{
       _id,
       title,
       slug,
       "dish": dish->{_id, name},
       fresh,
       stock,
       directions,
       "related": related[]->{title},
       "book": book->{_id, name, author},
       info,
       "tag": tag[]->{_id, name, category->{_id, name}},
      }
)
it returns the complete fresh array including <quantity, unit, ingredienttag: {_ref: "5f3bdac9e6490b05c8ba48ae", _type: "reference"}>.
Obviously I want to return the name of the referenced "tag", instead of _ref.
So instead of <fresh,> I do < "fresh": fresh[]->{quantity, unit, ingredienttag->{name}},> but it just returns an array of empty objects.
What can be wrong here? (all the rest is working as expected)
Feb 24, 2021, 12:03 PM
Hi Pieter. It looks like the
fresh[]
array doesn't contain any references, but a direct application of ingredienttag. If that's how you want it to be, you could try removing the dereferencing operator (
->
) after
fresh[]
to see if that works.
Feb 24, 2021, 2:13 PM
Thanks, but please read carefully. I did "fresh": fresh[]->{quantity, unit, ingredienttag->{name}}, as I mentioned before, but it just returns an array of empty objects. These are the 3 different results:fetch1 = fresh,
console1 = fresh: 0
:ingredienttag: {_ref: "5f3bdac9e6490b05c8ba48ae", _type: "reference"}fetch2 = "fresh": fresh[]->,
console2 = fresh: Array(2) 0: null 1: null
fetch3 ="fresh": fresh[]->{ingredient, quantity, unit, ingredienttag->{name}},
console3 = fresh: Array(2) 0: {} 1: {}
Nothing works to get the name of "ingredienttag".
Feb 24, 2021, 2:42 PM
I am getting the same problem following the "Minimal Example" at Sanity. I want to fetch an array of crewMembers, with their jobs and names together. I tried: "crewMembers": crewMembers[]->{job, person->name}, but this does not work. What is the correct query?
Feb 24, 2021, 3:11 PM
Thanks, but please read carefully. I did “fresh”: fresh[]->{quantity, unit, ingredienttag->{name}}, as I mentioned before
Please accept my apologies, Pieter. What I haven’t yet seen you try in those three examples is removing the dereferencing operator,
->
, after
fresh[]
(in your first fetch example it’s removed, but only after
fresh
). Since
fresh[]
is an array of type
ingredienttag
and not an array of references, I don’t expect you’d want to include it.
I created a prototype of your schema and this worked for me:


*[_type == "recipe"] {
  _id,
  title,
  slug,
  "fresh": fresh[] {
    quantity,
    unit,
    ingredienttag->{name}
  }
}
Feb 24, 2021, 3:12 PM
Hey Geoff, 1000x thanks! You saved my day.
Feb 24, 2021, 3:48 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?

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
Hey all - what’s the quickest way to batch delete all documents of a certain type? It looks like the `sanity` cli tool allows...Jan 29, 2021
How to turn PortableText into plain text in Javascript?Dec 7, 2020
Rendering nested block contentJan 18, 2021
In the tutorial, there is samples of what the API returns as JSON. Is there a way to view these ?Jan 11, 2021
Warning: Data for Page Exceeds the Threshold of 128 kBAug 17, 2022
List Arrays - Is it Possible to Fill a List Array Within a Schema Asynchronously?Mar 16, 2021
Custom Document Views - Is it Even Possible to Use the Structure Builder?Apr 21, 2022
How to get the number of nested/children components in a GROQ query.Jan 19, 2024
TypeError: Cannot Read Properties of Null (Reading 'jsonType')Sep 28, 2022
Error in custom validation rule for schema in Slack threadAug 18, 2023

Related contributions

Turbo Start Sanity
- Template

The battle-tested Sanity template that powers Roboto Studio's websites

Go to Turbo Start Sanity

Schema UI - Next.js Sanity Starter
- Template

A Next.js starter template with Next.js 15, Tailwind CSS, shadcn/ui, and Sanity CMS with Live Editing. Get production-ready React components with matching Sanity schemas and queries. Build dynamic pages faster while keeping full control over customization.

Serge Ovcharenko
Go to Schema UI - Next.js Sanity Starter

The Swaddle
- Made with Sanity

A new brand identity to represent a more mature company, to signify The Swaddle’s evolution from publisher to production house, combined with an easier to navigate platform that can surface multiple content types - drawing readers through The Swaddle’s content offering.

Nightjar
Go to The Swaddle