简体   繁体   中英

Check if MySQL row exists - Node.JS

Previous post I will refer to later

I am making a Discord bot which uses MySQL, but that shouldn't matter, I am trying to do a blacklist database so users in it can't use my bot

This is what I got so far:

con.query("SELECT EXISTS( SELECT 1 FROM `blacklist` WHERE `id` = '"+message.author.id+"')", function(error, result, field) {
    if(error) throw error;
});

And this kinda works, this is my output

[ RowDataPacket {
'EXISTS( SELECT 1 FROM `blacklist` WHERE `id` = \'227090776172134400\')': 1 } ]

And the last digit works like a boolean, 1 if the row exists, 0 if it does not But my problem is, that I can't seem to figure out how to check if it's a zero or not because it's an object

Why can't you make the query string a variable that you can later query on. For example:

let conStr = "SELECT EXISTS( SELECT 1 FROM `blacklist` WHERE `id` = '"+message.author.id+"')";
con.query(conStr, function(error, result, field) {
    if(error) throw error;
    console.log(result[conStr]); //--> 1
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM