👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with using the sanity client to patch plain text into a portable text field causing errors in the studio.

7 replies
Last updated: Jun 29, 2023
Using the sanity client to patch plain text into a portable text field, but I am encountering some errors in the studio after the patch is committed. The studio crashes but the changes are actually saved to the field, and everything seems fine after refreshing the page.
Jun 29, 2023, 9:51 PM
Here is the function I am using to convert plain text to portable text:
export function buildPortableTextField(plainText) {
  if (!plainText || plainText.length === 0) {
    return undefined;
  }

  return plainText.split('\n').map(text => ({
    style: 'normal',
    _type: 'block',
    children: [
      {
        _type: 'span',
        marks: [],
        text,
      },
    ],
    markDefs: [],
  }));
}
Jun 29, 2023, 9:52 PM
And here are the errors I'm getting:
Jun 29, 2023, 9:54 PM
Hi
user A
. Given the errors you’re seeing I’m not yet sure if it’s the cause, but I see there are no `_key`s being added. When you refresh the Studio and inspect the JSON, do you see
_key
values on the objects of
_type: 'block'
and
_type: 'span'
?
Jun 29, 2023, 10:03 PM
user A
Thanks for the reply. Yes I do see
_key
added to block and span. Here is the result after the patch:
  "venueNotes": [
    {
      "_key": "BRtXrh1tOMb9vrrsTlm25B",
      "_type": "block",
      "children": [
        {
          "_key": "BRtXrh1tOMb9vrrsTlm2Ap",
          "_type": "span",
          "marks": [],
          "text": "Note line 1"
        }
      ],
      "markDefs": [],
      "style": "normal"
    },
    {
      "_key": "BRtXrh1tOMb9vrrsTlm2GT",
      "_type": "block",
      "children": [
        {
          "_key": "BRtXrh1tOMb9vrrsTlm2M7",
          "_type": "span",
          "marks": [],
          "text": "Note line 2"
        }
      ],
      "markDefs": [],
      "style": "normal"
    }
  ],
Jun 29, 2023, 10:13 PM
Question about keys got me wondering... I just added this into the function that builds out the array: https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/block-tools#normalizeblockblock-options and that seems to fix it. So perhaps the keys are required for the transaction to work, even though they eventually get added automatically?
Jun 29, 2023, 10:50 PM
So perhaps the keys are required for the transaction to work, even though they eventually get added automatically?
Yes, I think this is exactly right.
Jun 29, 2023, 10:57 PM
Good to know. Thanks!
Jun 29, 2023, 10:57 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?