Error with undefined properties in Sanity.io project

1 replies
Last updated: Jun 6, 2023
Need help with an error, it was working well before and now it isn't. I am not sure why.
Error: Cannot read properties of undefined (reading 'includes')
TypeError: Cannot read properties of undefined (reading 'includes')

I think it might be from these lines of code:
{
name: 'projectType',
title: 'Type',
type: 'array',
of: [{type: 'string'}],
options: {
list: [
{title: 'Building', value: 'building'},
{title: 'Districts', value: 'districts'},
],
layout: 'radio',
},
description:
'These are the types of properties in a project. Building will have number of floors and units. Districts will have number of villas/townhouses.',
},
{
name: 'numFloors',
title: 'Number of Floors',
type: 'number',
hidden: ({parent}: {parent: {projectType: string[]}}) =>
!parent.projectType.includes('building'),
description: 'It indicates the number of floors in the building.',
},
{
name: 'numUnits',
title: 'Number of Units',
type: 'number',
hidden: ({parent}: {parent: {projectType: string[]}}) =>
!parent.projectType.includes('building'),
description: 'It indicates the number of units in the building.',
},
{
name: 'numOfHouses',
title: 'Number of Villas/Townhouses',
type: 'number',
hidden: ({parent}: {parent: {projectType: string[]}}) =>
!parent.projectType.includes('districts'),
description: 'It indicates the number of villas or townhouses in the district.',
},
Jun 6, 2023, 12:27 AM
When the document is first created, it only has a
_type
and an
_id
(
projectType
doesn’t exist in the document), so
parent.projectType
will fail. You should be able to avoid this error by using optional chaining —that is, changing all the instances of
!parent.projectType.includes(...)
to
!parent.projectType?.includes(...)
.
Jun 6, 2023, 4:04 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?