👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Help needed with referencing subcategories in a blog schema in Sanity Studio

2 replies
Last updated: Apr 15, 2022
Hello Everyone, I have two schemas saved as documents one is blog and other one is category. category schema has subcategory as an array. I am getting an error on the highlighted code in bold part in blog schema. Could anyone help me out by telling what went wrong in the bold portion of blog schema(schema 1). Objective: I want to get subcategories in subcategory field of blog schema as drop down options from category schema in sanity studio. Schema -1
export default {

title: 'Blog',

name: 'blog',

type: 'document',

fields: [

{

title: 'Title',

name: 'title',

type: 'string',

},

{

title: 'Description',

name: 'description',

type: 'string',

},


{

title: 'Tags',

name: 'tags',

type: 'string',

},


{

name: 'category',

title: 'Category',

type: 'reference',

to: [{ type: 'category' }],

},


{

name: 'subcategory',

title: 'subcategory',

type: 'reference',

to: [{ type: 'category', of: [{type: 'array', to: [{ type: 'subcategory' }],}] }],

},



],

};



schema -2


export default {

title: 'Category',

name: 'category',

type: 'document',

fields: [

{

title: 'Name',

name: 'name',

type: 'string',

},


{

title: 'Subcategory',

name: 'subcategory',

type: 'array',


of: [{

type: 'object',

fields: [

{

title: 'Name',

name: 'name',

type: 'string',

to: [{ type: 'subcategory' }],

},


],

}],



},



],

};
Apr 14, 2022, 12:34 PM
the best would be if you have subcategories as separate document, and each subcategory should have parent category
Apr 14, 2022, 12:51 PM
Thanks
user M
. Thanks
user Q
I will project subcategory as a separate document schema. I learned a new thing from you , Thank you so much.
Apr 15, 2022, 3:38 AM

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?