✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

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?
Sep 7, 2022, 10:11 AM
If you need a
name
from a groq query, you would need to expand the reference.
director-> { name }
Sep 7, 2022, 10:14 AM
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'}]
          }
Sep 7, 2022, 10:24 AM
Cause I need to see the name in sanity studio not in front end.
Sep 7, 2022, 10:38 AM
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
Sep 7, 2022, 10:47 AM
What are you trying to do and what are you currently facing as a problem please? 🙂
Sep 7, 2022, 12:03 PM
we have predefined categories. On my colleges pc we can see the category that we chose while on mine we don't see it.
Sep 7, 2022, 12:07 PM
What does the schema look like for that field?
Sep 7, 2022, 12:09 PM
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",
      }
    ]
}
Sep 7, 2022, 12:20 PM
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 😅
Sep 7, 2022, 12:22 PM
And the schema for
category
?
Sep 7, 2022, 12:23 PM

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

}
Sep 7, 2022, 12:25 PM
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 }
    }
}
Sep 7, 2022, 12:27 PM
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 }
        },
      }

}
Sep 7, 2022, 12:33 PM
I have the exact same code as Odeta and it works fine for me.edit: Im on Macbook
Sep 7, 2022, 12:34 PM
Empty cache in browser maybe?
Sep 7, 2022, 12:34 PM
did not help
Sep 7, 2022, 12:39 PM
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'
      }
}

Sep 8, 2022, 4:35 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?