-2

I need your help guys. I am in charge of building a workspace in Notion for my team where we can cast our votes on ideas. I've to use the Person property for a member to put their names on it if they like the ideas and use formulas to count. But it has some weaknesses as they can also put another member name in that too.

So my idea is to make a button so that when members click it, their name will be automatically added to the Person property in the database.

But I'm just a newbie in code, and I'm guessing it has something to do with Notion's API. Can anyone help me with any resources I can watch or read to complete this task?

Thank you so much

Hoko NgPh
  • 1
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 28 '23 at 17:47

1 Answers1

1

Try the following approach:

    // 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);
});
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
SHresTho12
  • 24
  • 6