Migration to add block to modular content field?

I’m attempting to update a modular content field to add an existing block model, however only adding the id for this new block erases all other existing blocks.

I retrieved the IDs for existing blocks and attempted to update the field by passing a new array for richTextBlocks itemTypes but I get a 500 error.

Is there a way to add a new block to a modular content field via a migration?

Hello @operations,

if you want to add a block to a modular content you should pass the full array of blocks, yes.

But if you are getting a 500, can you please share some code so that I can have a look maybe?

1 Like

Sure, here’s what I’ve got:

  const contactForm = await client.itemTypes.find('contactform').then(console.log('looked up contact form'));
  const existingBlocks = await client.fields.find('workdetailpage::components').then(console.log('found workdetailpage blocks'));

  //TODO : need to get list of existing blocks in modular content field when updating...
  var existingBlocksList =  existingBlocks.validators.richTextBlocks.itemTypes;
  //TODO : then add it to that array
  const updatedBlocksIdList = existingBlocksList.push(contactForm.id);

  //update component list to add contactForm
  const modularContent = await client.fields.update('workdetailpage::components', {
    fieldType: "rich_text",
    apiKey: "components",
    validators: {
      richTextBlocks: {
        itemTypes: updatedBlocksIdList,
  }).then((field) => {
  .catch((error) => {

and what’s the error you are getting sorry?

⠹ Running 1600217136_AddContactform.js...["301617","301616","301615","301614","301583","301613","301611"]
⠸ Running 1600217136_AddContactform.js...ApiException: 500 Internal Server Error
    at _callee$ (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/lib/utils/generateClient.js:205:45)
    at tryCatch (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/node_modules/regenerator-runtime/runtime.js:293:22)
    at Generator.next (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/node_modules/regenerator-runtime/runtime.js:118:21)
    at asyncGeneratorStep (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/lib/utils/generateClient.js:30:103)
    at _next (/Users/robb/.asdf/installs/nodejs/14.3.0/.npm/lib/node_modules/datocms-client/lib/utils/generateClient.js:32:194)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  body: { data: [ [Object] ] },
  headers: undefined,
  statusCode: 500,
  statusText: 'Internal Server Error'
⠇ Running 1600217136_AddContactform.js...{
  id: '1343829',
  label: 'components',
  fieldType: 'rich_text',
  localized: false,
  defaultValue: null,
  apiKey: 'components',
  hint: null,
  validators: { richTextBlocks: { itemTypes: [Array] } },
  appeareance: {
    editor: 'rich_text',
    parameters: { startCollapsed: true },
    addons: []
  appearance: {
    editor: 'rich_text',
    parameters: { startCollapsed: true },
    addons: []
  position: 5,
  itemType: '301594',
  fieldset: null

could you please try printing out the data inside the body of the exception? thank you very much

Was a JS issue on my part – I do need to send the full array, but I was mistakenly converting my array to an integer in the process of trying to push a new value into the existing array.

1 Like