Skip to content
Advertisement

.js + sql variables cant get the sum

It just gives the two variables put together instead of the sum of the two

Ex. 1 + 2 = 12 instead of 1 + 2 = 3 or args[1] + money = args[1]money instead of new value

const { RichEmbed } = require('discord.js');

module.exports = {

    name: 'givemoney',

    description: 'Gives User Money',

    usage: '<username | id | mention | amount>',

    run: (client, message, args, con) => {
        const target = message.mentions.users.first() || message.guild.members.get(args[1]) || message.author;


        if (!args[0]) {
            return message.channel.send('This is args 1');
        }
        if (!args[1]) {
            return message.channel.send('This is args 2');
        }

        con.query(`SELECT money FROM account WHERE id = '${target.id}'`, (err, rows) => {
            if(err) throw err;


             const money = rows[0].money;
             parseInt(args[1].value, 10) + money;

            const sql = `UPDATE account SET money = '${money}' WHERE id = '${target.id}'`;

            con.query(sql);

            message.channel.send(`You have given ${target} $${args[1]}, They now have $${money}`);
        });

    },
};

Advertisement

Answer

I can’t see where you are doing this operation and assigning it to a variable, maybe you mean this line parseInt(args[1].value, 10) + money;? this line is not assigned to a variable, but anyway the issue you are face seems to be related to string concatenation instead of sum numbers just add a + sign just right before your variables like this

let args = ["1", "2", "3"];
let money = "3";
let newVal = (+args[1]) + (+money);

console.log(newVal);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement