Home » Javascript » javascript – .js + sql variables cant get the sum

javascript – .js + sql variables cant get the sum

Posted by: admin February 24, 2020 Leave a comment

Questions:

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}`);
        });

    },
};
How to&Answers:

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);

Answer:

If i understood your question, you can do in this way:

var money=5;
args=[1,2,3,4,5];
console.log((+args[1])+(+money));

You in this way get ,in this example, the sum 5+2=7.
Hope this helps