一个可以在Notion数据库的Person属性中添加人名的按钮

问题描述 投票:0回答:1

我需要你们的帮助。我负责我的团队的一个项目。这需要我制作一个按钮,以便当成员单击它时,他们的名字将自动添加到数据库中的 Person 属性中。

我只是一个代码新手,我猜它与Notion的API有关。任何人都可以帮助我使用我可以观看或阅读的任何资源来完成此任务吗?

非常感谢

javascript html css button notion-api
1个回答
1
投票
    // Load the Notion JavaScript library
const { Client } = require('@notionhq/client');

// Initialize the client with your integration token
const notion = new Client({ auth: process.env.NOTION_API_KEY });

// Retrieve the database ID and the ID of the Person property
const databaseId = '<your-database-id>';
const personPropertyId = '<your-person-property-id>';

// Function to add a person name to the database
async function addPersonName(name) {
  // Create a new record in the database with the person name in the Person property
  const response = await notion.pages.create({
    parent: {
      database_id: databaseId,
    },
    properties: {
      [personPropertyId]: {
        type: 'person',
        person: [{ email: name }],
      },
    },
  });

  console.log(`Added ${name} to database.`);
}

// Attach the function to a button on a webpage
const addButton = document.querySelector('#add-button');
addButton.addEventListener('click', () => {
  const name = prompt('Enter person name:');
  addPersonName(name);
});
© www.soinside.com 2019 - 2024. All rights reserved.