Manage Poll Options

Guide: Managing Poll Options with pollz-js SDK

Prerequisites

Before you proceed with managing poll options using the pollz-js SDK, ensure that you have already initialized the SDK and created a poll by following the relevant guides in the "Guides" section.

Manage Poll Options

You can add, delete, or rename poll options with ease using the pollz-js SDK.

Step 1: Import the SDK

In your Node.js application, ensure you have already imported the pollz-js SDK:

const { PollzSDK } = require('pollz-js');

Step 2: Add a Poll Option

Use the addOption method provided by the pollOptions module of the SDK to add a new option to a specific poll. Provide the poll ID and the label for the new option.

const pollz = new PollzSDK();

// Replace 'your-app-id' and 'your-app-secret' with actual credentials
const initInput = {
  appId: 'your-app-id',
  appSecret: 'your-app-secret',
};

// Initialize the SDK
await pollz.init(initInput);

// Replace with the actual poll ID
const pollId = 1;

try {
  const newOption = await pollz.pollOptions(pollId).addOption('New Option');
  console.log('New Poll Option:', newOption);
} catch (error) {
  console.error('Failed to add poll option:', error.message);
}

Replace 'your-app-id' and 'your-app-secret' with the actual credentials obtained from the Pollz platform. Adjust the pollId and the label for the new option according to your application's context.

Step 3: Delete a Poll Option

Use the deleteOption method provided by the pollOptions module of the SDK to delete a poll option. Provide the option ID as an argument.

// Replace with the actual option ID
const optionIdToDelete = 1;

try {
  const result = await pollz.pollOptions(pollId).deleteOption(optionIdToDelete);
  console.log('Poll Option Deleted:', result);
} catch (error) {
  console.error('Failed to delete poll option:', error.message);
}

Step 4: Rename a Poll Option

Use the renameOption method provided by the pollOptions module of the SDK to rename a poll option. Provide the option ID and the new name for the option.

// Replace with the actual option ID
const optionIdToRename = 1;
const newName = 'Updated Option';

try {
  const updatedOption = await pollz.pollOptions(pollId).renameOption(optionIdToRename, newName);
  console.log('Poll Option Renamed:', updatedOption);
} catch (error) {
  console.error('Failed to rename poll option:', error.message);
}

Congratulations! You have successfully managed poll options by adding, deleting, and renaming them using the pollz-js SDK. Explore more SDK features and functionalities to customize your application's polling experience. Refer to the relevant guides for detailed instructions on other SDK capabilities.

Last updated