I have created an object with 3 reference fields - categories, subcategories, and types. I want the subcategories to show subcategories related to the selected category.
{
title: "Product Category",
name: "category",
type: "object",
fields: [
{
name: "categories",
type: "reference",
to: [
{
type: "categories",
},
],
},
{
name: "subcategories",
type: "reference",
to: [
{
type: "subcategories",
},
],
},
{
name: "types",
type: "reference",
to: [
{
type: "types",
},
],
},
],
},
I tried the filter
option as mentioned in docs but can't seem to get it working so I removed it.
This is how I defined the categories
and subcategories
.
// categories.js
export default {
name: "categories",
type: "document",
title: "Categories",
fields: [
{
name: "category",
type: "string",
title: "Category name",
},
{
name: "slug",
title: "Slug",
type: "slug",
description: "URL friendly name",
options: {
source: "category",
maxLength: 96,
},
},
{
title: "Description",
name: "description",
type: "text",
},
],
};
// subcategories.js
export default {
name: "subcategories",
type: "document",
title: "Subcategories",
fields: [
{
name: "subcategory",
type: "string",
title: "Subcategory name",
},
{
name: "slug",
title: "Slug",
type: "slug",
description: "URL friendly name",
options: {
source: "subcategory",
maxLength: 96,
},
},
{
name: "categories",
type: "reference",
to: [
{
type: "categories",
},
],
},
],
};
Any help is appreciated.
categories
object? Thefilter
field should work, but maybe only directly on thereference
type. Hard to see when we can't see the definition ofsubcategories
andcategories
.