Troubleshooting missing category name in Sanity Studio

17 replies
Last updated: Sep 8, 2022
Hey. Can someone explain why I am not getting category name from reference.
tag:

export default {
    name : "customTag",
    title: "Tag",
    type : "object",
    fields : [

        {
            title: "Tag",
            name: "tag",
            type : "string",

        },
        {
            title: 'Director',
            name: 'director',
            type: 'reference',
            to: [{type: 'category'}]
          }
    ] 
}

Have I forgot to add something?
AI Update

Based on your schema, the issue is likely that you're not dereferencing the reference field when querying your data. Your schema is set up correctly, but references in Sanity store only the document ID by default - you need to explicitly tell GROQ to fetch the actual referenced document.

When you query your customTag object without dereferencing, you'll get something like:

{
  "tag": "some tag",
  "director": {
    "_ref": "category-id-123",
    "_type": "reference"
  }
}

To get the actual category name, you need to use the reference access operator (->) in your GROQ query:

*[_type == "yourDocumentType"] {
  customTag {
    tag,
    director->  // This resolves the reference
  }
}

Or if you only want specific fields from the category:

*[_type == "yourDocumentType"] {
  customTag {
    tag,
    director-> {
      name,
      title
    }
  }
}

If this is appearing in an array, you'd do:

*[_type == "yourDocumentType"] {
  customTag[] {
    tag,
    director->
  }
}

Your schema definition is fine - the -> operator is what tells GROQ to fetch the referenced category document instead of just returning the reference ID. Without it, you only get the internal _ref field, not the actual category data.

If you need a
name
from a groq query, you would need to expand the reference.
director-> { name }
Can you specify where I should do that? I haven't seen this syntax before. Not sure where in code it belongs
I tried like so but it did not work so I assume it's not where it belongs


 {
            title: 'Director',
            name: 'director-> { name }',
            type: 'reference',
            to: [{type: 'category'}]
          }
Cause I need to see the name in sanity studio not in front end.
You need it in the list where you reference it in Sanity?
Then in you category schema, you will need to change the preview i would think.


https://www.sanity.io/docs/previews-list-views
What are you trying to do and what are you currently facing as a problem please? 🙂
we have predefined categories. On my colleges pc we can see the category that we chose while on mine we don't see it.
What does the schema look like for that field?
export default {
    name:'filterTags',
    title: 'Filter Tags',
    type: 'document',
    fields: [
      {
        name: "categoryTag",
        title: "Category Tag",
        type: "reference",
        to: [{type: 'category'}]
        
      },
      {
        name: "tagName",
        title: "Tag Name",
        description: "",
        type: "string",
      }
    ]
}
I assume that it's the server connection problem. No one else has this problem in the team and they see the description in sanity studio. But I could be very wrong as well 😅
And the schema for
category
?

export default {
    name: 'category',
    title: 'Category Folder',
    type: 'document',
    fields: 
    [
        {
          name: "categoryTitle",
          title: "Category Title",
          type: "string",
          description: '',
        },
        
      
        ]

}
Ah yes, that makes sense. By default, the preview relies on a
title
property I think. Because your field is named differently, you’ll have to adjust the preview from your
category
document.
export default {
  …
  preview: {
    select: { title: 'categoryTitle' },
    prepare({ title }) {
      return { title }
    }
}
I did this but it didn't help. Also every one else works on the same repo and they see the category name inside the box without this extra block of code
export default {
    name: 'category',
    title: 'Category Folder',
    type: 'document',
    fields: 
    [
        {
          name: "categoryTitle",
          title: "Category Title",
          type: "string",
          description: '',
        },
      ],
      preview: {
        select: { title: 'categoryTitle' },
        prepare({ title }) {
          return { title }
        },
      }

}
I have the exact same code as Odeta and it works fine for me.edit: Im on Macbook
Empty cache in browser maybe?
did not help
Hello
user E
you don’t need to select and prepare and then return if you use this simple structure:

export default {
    name: 'category',
    title: 'Category Folder',
    type: 'document',
    fields: 
    [
        {
          name: "title",
          title: "Category Title",
          type: "string",
        },
      ],
      preview: {
        title: 'title'
      }
}

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?