Facing Notion API problem while trying updating databases properties: can't update checkbox

2.7k Views Asked by At

with Notion API and node.js, I'm trying to update database properties, especially the checkbox property, for my routine ToDo tasks checker.

But I couldn't find the correct way to do so. Error messages say my setting of the checkbox property isn't correct, but I couldn't find the right way in the Notion API documents.

Here's my code:

const { Client } = require("@notionhq/client");
require("dotenv").config();
const notion = new Client({ auth: process.env.NOTION_KEY });
const databaseId = process.env.NOTION_DATABASE_ID;

const uncheckRoutineTasksTodo = async () => {
    const response = await notion.databases.update({
        database_id: databaseId,
        icon:{//Worked
            type:"emoji",
            emoji:""
        },
        properties: {
            Done: {
                checkbox: false,
            },
        },
    });
    console.log(response);
};
uncheckRoutineTasksTodo();

And error messages are:

@notionhq/client warn: request fail {
  code: 'validation_error',
  message: 'body failed validation. Fix one:\n' +
    'body.properties.Done.number should be defined, instead was `undefined`.\n' +
    'body.properties.Done.formula should be defined, instead was `undefined`.\n' +
    'body.properties.Done.select should be defined, instead was `undefined`.\n' +
    'body.properties.Done.multi_select should be defined, instead was `undefined`.\n' +
    'body.properties.Done.relation should be defined, instead was `undefined`.\n' +
    'body.properties.Done.rollup should be defined, instead was `undefined`.\n' +
    'body.properties.Done.title should be defined, instead was `undefined`.\n' +
    'body.properties.Done.rich_text should be defined, instead was `undefined`.\n' +
    'body.properties.Done.url should be defined, instead was `undefined`.\n' +
    'body.properties.Done.people should be defined, instead was `undefined`.\n' +
    'body.properties.Done.files should be defined, instead was `undefined`.\n' +
    'body.properties.Done.email should be defined, instead was `undefined`.\n' +
    'body.properties.Done.phone_number should be defined, instead was `undefined`.\n' +
    'body.properties.Done.date should be defined, instead was `undefined`.\n' +
    'body.properties.Done.checkbox should be an object, instead was `false`.\n' +
    'body.properties.Done.created_by should be defined, instead was `undefined`.\n' +
    'body.properties.Done.created_time should be defined, instead was `undefined`.\n' +
    'body.properties.Done.last_edited_by should be defined, instead was `undefined`.\n' +
    'body.properties.Done.last_edited_time should be defined, instead was `undefined`.\n' +
    'body.properties.Done.name should be defined, instead was `undefined`.'
}

As messages suggest, I tried to transform the checkbox property to object, but the result was the same.

properties: {
        Done: {
            checkbox: {
                checkbox:false
                }
           },
    },
body.properties.Done.checkbox should be an object, instead was `false`. 

Does someone know how to set checkbox property correctly? Please bail me out…

3

There are 3 best solutions below

0
On

Exploring in the notion.databases.update()type definition, there're types info about each params. And in checkbox property, I find these below:

declare type UpdateDatabaseBodyParameters = {
   | null | {
        checkbox: EmptyObject;
        type?: "checkbox";
        name?: string;
    } 
}

checkbox param should be EmptyObject. That's why the compiler shows the error I got, body.properties.Done.checkbox should be an object.

Then, I digged into EmptyObject type definition, and that's here:

declare type EmptyObject = Record<string, never>;

Record<key, value> is an object type definition that has types {key : value}.

Getting back to the issue, EmptyObject is an object that accepts {string: never}. And indeed, type never does not accept any types. Therefore I can set nothing inside the object as a property.

In conclusion, I couldn't find a way to mutate the checkbox property already being on the exixsed DB through API.

If someone know the appropriate way, please let me know...

Thx.

0
On

You want to use notion.pages.update() instead notion.databases.update(). Each entry of a database is called a 'page', so you want to modify that, rather than the database itself.

As per the Notion API docs, an example would be:

const { Client } = require('@notionhq/client');

const notion = new Client({ auth: process.env.NOTION_API_KEY });

(async () => {
  const pageId = {YOUR_PAGE_ID};
  const response = await notion.pages.update({
    page_id: pageId,
    properties: {
      'In stock': {
        checkbox: true,
      },
    },
  });
  console.log(response);
})();
0
On

I had had the same Problem and the solution to past an empty object. So for example properties: { Done: { checkbox: {}, },

And in the update the property can be true or false