I'm trying to make a poll command for my discord bot, but it keeps giving me the error: "TypeError: Cannot read property 'push' of undefined"

Viewed 49

The title pretty much says it all, I'm wondering if anyone could help me. I'm using discord v11.5.1

Here is my code:

exports.run = async (bot, message) => {
    const options = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""];
    const regex = /(["'])((?:\\\1|\1\1|(?!\1).)*)\1/g;
    const args = message.content.trim().split(/ +/g);
    let question, choices, content = [];
    let match;

    message.delete();
    for(let i=1;i<args.length;i++) {
        if(args[i].startsWith('"')) break;
        else question.push(args[i]);
    };

    question = question.join(" ");

    while(match = regex.exec(args.join(" "))) choices.push(match[2]);
    for(let i=0;i<choices.length;i++) content.push(`${options[i]} ${choices[i]}`);
    content = content.join("\n");

    message.channel.send(`${message.author} **started a poll**`, new(require("discord.js")).RichEmbed().setColor(0xbc13fe).setTitle(`:bar_chart: **Poll |** ${question}`).setDescription(content).setTimestamp()).then(async m => {for(let i=0;i<choices.length;i++) await m.react(options[i])});
}

Any help would be greatly appreciated! :)

2 Answers

When you write let question, choices, content = [] you create three variables. content will be an empty array, question and choices will be undefined.

If you want to initialise them all with empty arrays, you can do it like this:

let question = [];
let choices = [];
let content = [];

if below are all array, then you want to initialize it with empty array, then use = instead of comma ,

 let question = choices = content = [];

instead of

 let question, choices, content = [];
Related