尝试更新数据库属性时遇到概念 API 问题:"无法更新"复选框



与概念API和node.js,我试图更新数据库属性,特别是复选框属性,用于我的例行ToDo任务检查器。

但是我找不到正确的方法去做。错误消息说我的复选框属性设置不正确,但我在概念API文档中找不到正确的方法。

下面是我的代码:

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();

和错误信息是:

@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`.'
}

如消息所示,我尝试将checkbox属性转换为object,但结果是相同的。

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

有人知道如何正确设置复选框属性吗?请帮我一把……

您想使用notion.pages.update()而不是notion.databases.update()。数据库的每个条目被称为"页",所以你想修改的是它,而不是数据库本身。

根据概念API文档,一个例子是:

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);
})();

notion.databases.update()类型定义中探索,有关于每个参数的类型信息。在checkbox属性中,我发现以下内容:

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

复选框参数应为EmptyObject。这就是为什么编译器显示我得到的错误,body.properties.Done.checkbox should be an object

然后,我深入研究了EmptyObject类型定义,在这里:

declare type EmptyObject = Record<string, never>;

Record<key, value>是具有{key : value}类型的对象类型定义。

回到问题,EmptyObject是一个接受{string: never}的对象。实际上,类型never不接受任何类型。因此,我可以在对象内部不设置任何属性。

总之,我找不到一种方法来改变checkbox属性已经通过API存在于退出的DB上。

如果有人知道正确的方法,请告诉我…

Thx .

我有过同样的问题和解决方案,过去一个空对象。例如properties: { Done: { checkbox: {}, },

在更新中属性可以是true或false

最新更新