Closed ghost closed 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!");
Thanks for the quick reply. I'll certainly try this ^-^
~EDIT~ It worked. Thank you so much for this :D
Closing since this was just a JavaScript question and it was answered :)
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:
And this is the stack trace:
Does anyone know why this happens?