How to update a block record?

Hi,

How can I update a field of an existing block record? That block is used on “Specify the blocks allowed in this field” on a structured text field of a model.

I tried below codes, but it is not working:

client.items
    .update(block_record_id, {
      body: "test body",
    })
    .then((item) => {
      console.log(item);
    })
    .catch((error) => {
      console.error(error);
    });

Thanks,
Jyotirmoy Biswas

1 Like

Hello @jyoti

Since you are updating the block inside of a structured text, we would have to update the structured text field, and then there, update the block (if you have some doubts of how the creation/update of a structured text works, you can check out the documentation here: https://www.datocms.com/docs/content-management-api/resources/item/create#structured-text-fields)

Here is an example updating a structured text with a single block, with a single title:

client.items
  .update(RECORD_ID, {
    structeredTextWithABlock: {
      schema: "dast",
      document: {
        type: "root",
        children: [
          {
            type: "block",
            item: buildModularBlock({
              itemType: BLOCK_MODEL_ID,
              blockTitle: "Brand new and updated text!",
            }),
          },
        ],
      },
    },
  })
  .then((item) => {
    console.log(item);
  })
  .catch((error) => {
    console.error(error);
  });

Hope this helps!

Hi,

Thanks for your feedback. Can I fetch the records, those are using a block model record id?
For example, I am fetching all block model records belongs to a block model using below query:

const block_model_records = await client.items.all({
    filter: {
      type: **block_model_api_key**, // you can use models `api_key`s or models IDs
    },
  })

So I need to fetch the list of model records those are using block_model_records[0].id

Thanks again,
Jyotirmoy Biswas