📊
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: Voting in a Poll with pollz-js SDK
  • Prerequisites
  • Vote in a Poll
  1. Guides

Vote for a Poll

PreviousCreate a PollNextRetrieve a Poll

Last updated 1 year ago

Guide: Voting in a Poll with pollz-js SDK

Prerequisites

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

Vote in a Poll

Now that you have a poll created, users can cast their votes 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, PollTypes } = require('pollz-js');

Step 2: Vote in a Poll

Use the vote method provided by the polls module of the SDK to submit votes for a poll. Provide the poll type, poll ID, chosen option IDs, and the user ID.

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 and option IDs
const pollId = 1;
const optionIds = [1, 2];

// Replace with a unique user identifier
const userId = 'user-123';

try {
  await pollz.polls.vote(
    PollTypes.MultipleChoice,
    pollId,
    optionIds,
    userId
  );
  console.log('Vote submitted successfully!');
} catch (error) {
  console.error('Failed to submit vote:', error.message);
}

Replace 'your-app-id' and 'your-app-secret' with the actual credentials obtained from the Pollz platform. Adjust the pollId, optionIds, and userId according to your application's context.

Congratulations! Users can now cast their votes in the poll using the pollz-js SDK. Explore more SDK features and functionalities to enhance your application's polling experience. Refer to the relevant guides for detailed instructions on other SDK capabilities.

initialized the SDK
created a poll