Skip to content

TypeError: Cannot read property '0' of undefined #910

@ghost

Description

Hey guys,

I'm currently working on a twitch irc bot which uses a mysql database to store things like commands, points and regulars. But now I'm stuck.

I want to check if there is a record with channel and command because both together are unique and if I try to INSERT a duplicate it crashes the script, but for some reason it says that rows is undefinded.

This is my code:

if (msg.indexOf('!addcom') === 0) {
                if (typeof args[1,2] !== 'undefined' && args[1].trim() !== '') {
                    if(_.indexOf(modlist, user.username) >= 0) {
                        msg.split(" ");
                        args[2] = args.slice(2).join(" ");
                        connection.query('SELECT 1 AS check FROM commands WHERE channel = "'+channel+'" AND command = "'+args[1]+'"', function(err, rows, fields) {
                            if (rows[0].check === null) {
                                connection.query('INSERT INTO commands (channel, command, message) VALUES ("'+channel+'","'+args[1]+'","'+args[2]+'")');
                            }
                            else if (rows[0].check === 1) {
                                client.say(channel, "Der Befehl " +args[1]+ " existiert bereits!");
                            }
                            else {
                                client.say(channel, "Ein Fehler ist aufgetreten!");
                            }
                        });
                    }
                    else {
                        client.say(channel, user.username+", du bist kein Moderator und kannst diesen Befehl daher nicht ausführen!");
                    }
                }
                else {
                    client.say(channel, "Syntax Fehler: !addcom [!Befehl] [Nachricht]");
                }
            }

And this is the stack trace:

/home/NodeJS/node_modules/mysql/lib/protocol/Parser.js:82
        throw err;
              ^
TypeError: Cannot read property '0' of undefined
    at Query._callback (/home/NodeJS/scripts/twitchbot.js:153:22)
    at Query.Sequence.end (/home/NodeJS/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Query.ErrorPacket (/home/NodeJS/node_modules/mysql/lib/protocol/sequences/Query.js:93:8)
    at Protocol._parsePacket (/home/NodeJS/node_modules/mysql/lib/protocol/Protocol.js:271:23)
    at Parser.write (/home/NodeJS/node_modules/mysql/lib/protocol/Parser.js:77:12)
    at Protocol.write (/home/NodeJS/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/NodeJS/node_modules/mysql/lib/Connection.js:82:28)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:764:14)
    at Socket.emit (events.js:92:17)

Does anyone know why this happens?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions