使用notion javascript SDK更新notion中的页面时出现验证错误

Mur*_*rti 5 javascript notion-api

这是错误:

客户端警告:请求失败{代码:'validation_error',消息:'正文验证失败。修复一:应该定义 body.properties.body.id,而不是undefined。body.properties.body.name 应该被定义,而不是undefined。body.properties.body.start 应该被定义,而不是undefined。' 主体验证失败。修复一:应该定义 body.properties.body.id,而不是undefined。body.properties.body.name 应该被定义,而不是undefined。body.properties.body.start 应该被定义,而不是undefined

这是我的代码:

async function update() {
  const res_2 = await notion.databases.query({ database_id: `${databaseId}` });

  res_2.results.forEach(async (page, index) => {
    let property_id = page.properties.Excerpt.id;
    if (index === 0) {
      try {
        const res_3 = await notion.pages.update({
          page_id: page.id,
          properties: {
            [property_id]: {
              type: "rich_text",
              rich_text: {
                "content": "hey"
              }
            }
          }
        })

      } catch (err) {
        console.log(err.message)
      }
    }
  })
}
update();
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会出现身体验证错误以及它的起源。有修复吗?

Mur*_*rti 6

经过一番挖掘,我发现这个错误通常发生在请求正文中缺少参数时。根据官方的 Notion 文档,

请求正文与预期参数的架构不匹配。检查“消息”属性以获取更多详细信息。https://developers.notion.com/reference/errors

我没有正确构造该对象,这就是我收到此错误的原因。

正确的构造rich_text_object如下:

properties: {
  "Excerpt": {
    "rich_text": [
      {
        "type": "text",
        "text": {
          "content": "hello"
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以在这里找到更多信息:https://developers.notion.com/docs/working-with-page-content#creating-a-page-with-content