Notion API – 创建页面的问题

huangapple go评论64阅读模式
英文:

Notion API - Issue with creating pages

问题

在查阅了Notion文档之后,我无法弄清楚为什么我一直收到400状态码的返回。我的代码如下:

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

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

async function main() {
  try {
    await notion.pages.create({
      parent: {
        type: "database_id",
        database_id: process.env.DB_ID,
      },
      properties: {
        Name: [
          {
            text: {
              content: "测试名称",
            },
          },
        ],
        Link: {
          url: "www.google.com",
        },
        Notes: [
          {
            text: {
              content: "示例注释",
            },
          },
        ],
        Year: [
          {
            text: {
              content: "2020",
            },
          },
        ],
      },
    });
  } catch (error) {
    console.error(error);
  }
}

main();

API对于文本字段没有问题。问题出在名为Link的url列上,我收到以下错误:

body.properties.Link.id应该被定义,而不是`undefined`。
body.properties.Link.name应该被定义,而不是`undefined`。
body.properties.Link.start应该被定义,而不是`undefined`。

此外,似乎只要我不使用文本列,就会出现相同的错误。我在关系和复选框上也遇到了这个错误。这里是我一直在参考的访问页面属性的文档。

如果我使用这个示例中创建页面标题的模板,我也会得到相同的错误:

        Name: {
          title: [
            {
              text: {
                content: "Tuscan Kale",
              },
            },
          ],
        },

我已经检查了环境变量和集成权限,多次确认过了。

英文:

After scouring the Notion docs, I can't figure out why I keep getting a 400 status back. My code:

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

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

async function main() {
  try {
    await notion.pages.create({
      parent: {
        type: "database_id",
        database_id: process.env.DB_ID,
      },
      properties: {
        Name: [
          {
            text: {
              content: "Test name",
            },
          },
        ],
        Link: {
          url: "www.google.com",
        },
        Notes: [
          {
            text: {
              content: "sample notes",
            },
          },
        ],
        Year: [
          {
            text: {
              content: "2020",
            },
          },
        ],
      },
    });
  } catch (error) {
    console.error(error);
  }
}

main();

The API has no problem with the text fields. It's on the url column of key Link that I get the following error:

body.properties.Link.id should be defined, instead was `undefined`.
body.properties.Link.name should be defined, instead was `undefined`.
body.properties.Link.start should be defined, instead was `undefined`.

Further, it seems I get this same error as long as I'm not using a text column. I've gotten this error for relations and checkboxes as well. Here are the docs I've been referring to for accessing page properties.

I also get this same error if I use the template from this example for creating page titles:

        Name: {
          title: [
            {
              text: {
                content: "Tuscan Kale",
              },
            },
          ],
        },

I checked and double checked my environment variables and integration permissions.

答案1

得分: 1

很抱歉,API在这种情况下返回了一个误导性的错误,实际问题出在你指定标题和富文本属性的方式上。

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

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

async function main() {
	try {
		await notion.pages.create({
			parent: {
				type: 'database_id',
				database_id: process.env.DB_ID,
			},
			properties: {
				Name: {
					title: [
						{
							text: {
								content: '测试名称',
							},
						},
					],
				},
				Link: {
					url: 'www.google.com',
				},
				Notes: {
					rich_text: [
						{
							text: {
								content: '示例注释',
							},
						},
					],
				},
				Year: {
					rich_text: [
						{
							text: {
								content: '2020',
							},
						},
					],
				},
			},
		});
	} catch (error) {
		console.error(error);
	}
}

main();
英文:

Unfortunately the api returns a misleading error in this case, the issue is actually with the way you are specifying the title and rich_text properties.

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

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

async function main() {
	try {
		await notion.pages.create({
			parent: {
				type: 'database_id',
				database_id: process.env.DB_ID,
			},
			properties: {
				Name: {
					title: [
						{
							text: {
								content: 'Test name',
							},
						},
					],
				},
				Link: {
					url: 'www.google.com',
				},
				Notes: {
					rich_text: [
						{
							text: {
								content: 'sample notes',
							},
						},
					],
				},
				Year: {
					rich_text: [
						{
							text: {
								content: '2020',
							},
						},
					],
				},
			},
		});
	} catch (error) {
		console.error(error);
	}
}

main();

huangapple
  • 本文由 发表于 2023年7月31日 18:04:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定