0

I am running two database queries to retrieve data that I will outputting in a message embed. The queries are returning the proper rows when I just dump the entire result into the console. However, whenever I try to output the actual value for one of the rows, it displays as undefined in the message embed.

From what I've found based on examples, rows[0].somevalue should be outputting the correct results.

let mentionedUser = message.mentions.members.first();
let captainUser = client.users.find(user => user.id == `${mentionedUser.id}`);

con.query(`SELECT * FROM captains WHERE id = '${mentionedUser.id}';SELECT * FROM results WHERE captain = '${captainUser.username}'`, [2, 1], (err, rows) => {
    if(err) throw err;

    console.log(rows);

    const infoEmbed = new Discord.RichEmbed()
        .setColor("#1b56af")
        .setAuthor('Captain Information', client.user.displayAvatarURL)
        .setThumbnail('https://i.imgur.com/t3WuKqf.jpg')
        .addField('Captain Name', `${mentionedUser}`, true)
        .addField('Cap Space', `${rows[0].credits}`, true) // Returns undefined
    message.channel.send(infoEmbed);

    });

This is the console result

[ [ RowDataPacket {
      id: '91580646270439424',
      team_name: 'Resistance',
      credits: 85,
      roster_size: 2 } ],
  [ RowDataPacket { id: 'Sniper0270', captain: 'BTW8892', credits: 10 },
    RowDataPacket { id: 'Annex Chrispy', captain: 'BTW8892', credits: 5 } ] ]

In the code posted above, the expected output of rows[0].credits should output 85. No error codes are present, it just displayed as "undefined" in the message embed.

2 Answers 2

1

You are executing two queries inside a single query call. It looks like the mysql library returns an array of arrays in this scenario where the first value is the result of the first query and the second is the result of the second query. This is non standard. Normally you would either execute each query in its own query call or you would use a union to join the two queries into a single resultset.

Sign up to request clarification or add additional context in comments.

Comments

0

this is not the practical way to send query request , as query is a single statement excluding the bulk update , you cannot execute two different query using a single con.query , it is not a proper way. execute them separately

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.