Vote for a Poll
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 initialized the SDK and created a poll 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.
Last updated