mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.26k stars 2.53k forks source link

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

Closed ghost closed 9 years ago

ghost commented 9 years ago

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?

ZJONSSON commented 9 years ago

When a valid SQL query finds no matching records, it will return an empty array []. In that case rows[0] does not exists (i.e. is undefined) and rows[0].check will result in a TypeError: Cannot read property '0' of undefined. You might want to use if (!rows.length) instead, i.e.:

  if (err)
    client.say(channel, "Ein Fehler ist aufgetreten!");  
  else if (!rows.length)
    connection.query('INSERT INTO commands (channel, command, message) VALUES ("'+channel+'","'+args[1]+'","'+args[2]+'")');
  else 
    client.say(channel, "Der Befehl " +args[1]+ " existiert bereits!");
ghost commented 9 years ago

Thanks for the quick reply. I'll certainly try this ^-^

~EDIT~ It worked. Thank you so much for this :D

dougwilson commented 9 years ago

Closing since this was just a JavaScript question and it was answered :)