0

How do I check if a data exists in a MySQL database? I have already managed to get the data but I just want to know how to do it: If this data does not exist.

My current code:

connection.query(`select builduhcelo from practiceplayers where uuid = '${uuid}'`, function (errbu, resultbu) {
    if(errbu) throw errbu;
    connection.query(`select nodebuffelo from practiceplayers where uuid = '${uuid}'`, function (errnd, resultnd) {
        if(errnd) throw errnd;
        connection.query(`select ironbuilduhcelo from practiceplayers where uuid = '${uuid}'`, function (erribu, resultibu) {
            if(erribu) throw erribu;
            let embed = new Discord.RichEmbed()
                .setAuthor(`Elorion.net`, `https://image.noelshack.com/fichiers/2019/06/7/1549795460-logo-elorionnetwork.png`)
                .setColor(color.elorion)
                .addField("Username", username)
                .addField("UUID", uuid)
                .addField("BuildUHC Elo", resultbu[0].builduhcelo)
                .addField("NoDebuff Elo", resultnd[0].nodebuffelo)
                .addField("IronBuildUHC Elo", resultibu[0].ironbuilduhcelo)
                .addField("Skin", `[Download](https://crafatar.com/skins/${uuid}.png)`)
                .setThumbnail(`https://crafatar.com/avatars/${uuid}.png?size=400&overlay=true`)
                .setFooter(`Ⓒ Elorion.net 2019. All rights reserved`);
            message.channel.send(embed)
        })
    })
})

I receive this error when the uuid does not exist in the database:

throw err; // Rethrow non-MySQL errors 
^ 

TypeError: Cannot read property 'builduhcelo' of undefined 
trincot
  • 317,000
  • 35
  • 244
  • 286
Niromash
  • 15
  • 1
  • 8

1 Answers1

0

The problem is that you are referencing resultbu[0] when that resultbu will (sometimes) be an empty array, so then resultbu[0].builduhcelo is not possible and triggers the error.

Secondly, you are nesting queries that you could have done in one go. Select all three columns in your SQL query.

Finally, add an if condition to verify that the result array is not empty:

connection.query(`select builduhcelo, nodebuffelo, ironbuilduhcelo 
                  from practiceplayers 
                  where uuid = '${uuid}'`, function (err, result) {
    if (err) throw errbu;
    if (result.length) { /* Only execute the rest when there is a match: */
        let embed = new Discord.RichEmbed()
            .setAuthor(`Elorion.net`, `https://image.noelshack.com/fichiers/2019/06/7/1549795460-logo-elorionnetwork.png`)
            .setColor(color.elorion)
            .addField("Username", username)
            .addField("UUID", uuid)
            .addField("BuildUHC Elo", result[0].builduhcelo)
            .addField("NoDebuff Elo", result[0].nodebuffelo)
            .addField("IronBuildUHC Elo", result[0].ironbuilduhcelo)
            .addField("Skin", `[Download](https://crafatar.com/skins/${uuid}.png)`)
            .setThumbnail(`https://crafatar.com/avatars/${uuid}.png?size=400&overlay=true`)
            .setFooter(`Ⓒ Elorion.net 2019. All rights reserved`);
        message.channel.send(embed)
    }
})
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you very, very much. I was close to the goal when I was doing the tests, I just didn't know where to put it: ```if (result.length)``` – Niromash Feb 16 '19 at 12:57
  • This is a new question, which also enters a different technology (non-SQL). Could you please ask this as a new question? If you formulate it well and tag it correctly, it will soon attract the right audience to it. – trincot Feb 19 '19 at 16:11
  • Hello, I recently changed to mongoDB, so how could I do it via mongoose? My document : http://prntscr.com/mn9fqt Sorry for the inconvenience, thank you in advance Sincerely – Niromash Feb 19 '19 at 16:20
  • Ok no problem trincot – Niromash Feb 19 '19 at 16:21