Array of PortableText blocks in Sanity schema causing length error

2 replies
Last updated: Dec 8, 2022
Quick question regarding PortableText. I want to have a field that is an array of portable text. For a single portable text, I would create an array of block
type: 'array',
of: [{type: 'block'}]
But if I want this field to be a repeater to allow admin to create multiple different portableText blocks, what I tried is create a component that is an array of block and then creating a field that is an array of that component. But I seem to get an error when doing that.


// PortableText component
export default defineType({
  name: 'portableText',
  title: 'Portable Text',
  type: 'array',
  of: [{
    type: 'block',
    styles: [...],
    marks: {...}
  }]
})

// Inside my other document
...
{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'array',
  of: [{type: 'portableText'}],
  group: 'content',
}
This gives me the following error when opening the page

Error: Cannot read properties of undefined (reading 'length')
TypeError: Cannot read properties of undefined (reading 'length')
    at ListArrayInput (<http://localhost:5173/node_modules/.vite/deps/chunk-RZPXVCEY.js?v=35974456:87010:364>)
    at renderWithHooks (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:16007:26>)
    at mountIndeterminateComponent (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:18736:21>)
    at beginWork (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:19691:22>)
    at beginWork$1 (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:23492:22>)
    at performUnitOfWork (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22940:20>)
    at workLoopSync (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22876:13>)
    at renderRootSync (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22855:15>)
    at recoverFromConcurrentError (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22477:28>)
    at performSyncWorkOnRoot (<http://localhost:5173/node_modules/.vite/deps/chunk-7SP4WFCQ.js?v=35974456:22619:28>)
AI Update

The issue you're encountering is that Sanity doesn't support nested arrays (arrays within arrays). When you define portableText as an array type and then try to include it in another array, you're creating a nested array structure, which causes the error.

The solution is to wrap your portable text array inside an object type, then create an array of those objects. Here's how to fix it:

Solution: Use an Object Wrapper

// PortableText component - change to an OBJECT, not array
export default defineType({
  name: 'portableTextBlock',
  title: 'Portable Text Block',
  type: 'object',  // Changed from 'array' to 'object'
  fields: [
    {
      name: 'content',
      title: 'Content',
      type: 'array',
      of: [{
        type: 'block',
        styles: [...],
        marks: {...}
      }]
    }
  ]
})

// Inside your other document
{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'array',
  of: [{type: 'portableTextBlock'}],
  group: 'content',
}

Now your admins can add multiple portable text sections, and each one will be independently editable.

Alternative: Define Inline

If you prefer not to create a separate schema file, you can define the object inline:

{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'array',
  of: [
    {
      type: 'object',
      name: 'textSection',
      fields: [
        {
          name: 'content',
          title: 'Content',
          type: 'array',
          of: [{type: 'block'}]
        }
      ]
    }
  ],
  group: 'content',
}

The fundamental rule to remember: array → object → array is valid, but array → array is not supported in Sanity's data structure. This limitation exists because of how the data store handles real-time collaboration and unique _key identifiers for array items.

Show original thread
2 replies
The Studio doesn’t support arrays-directly-in-arrays, so you need to wrap it in an object:
{
  name: 'grow_notes',
  title: 'Grow notes',
  type: 'object',
  fields: [{
   type: 'portableText'
   name: 'content', // grow_notes.content
  }],
  group: 'content',
}

Thanks!

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?