📊
Pollz
  • Introduction
  • Quickstart
  • Guides
    • Initializing your app
    • Create a Poll
    • Vote for a Poll
    • Retrieve a Poll
    • Manage Poll Options
    • Listen to update
    • Anonymous Poll
  • Components
    • React
    • React Native
    • Embedded
  • API Reference
    • init()
    • pollOptions()
      • addOption()
      • deleteOption()
      • renameOption()
    • anonymous
      • createAnonymousToken()
      • getAnonymousPoll()
      • voteAnonymously()
    • polls
      • create()
      • get()
      • getAll()
      • vote()
      • rename()
      • deleteOne()
      • listen()
    • pollTypes
      • getAll()
  • Tooling
    • Typescript
    • React
      • usePoll()
      • usePolls()
      • usePollz()
      • usePollTypes()
      • useAnonymousPoll()
Powered by GitBook
On this page
  • Guide: Managing Poll Options with pollz-js SDK
  • Prerequisites
  • Manage Poll Options
  1. Guides

Manage Poll Options

PreviousRetrieve a PollNextListen to update

Last updated 1 year ago

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 and 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.

initialized the SDK
created a poll