Search code examples
node.jsdiscord.jsbetter-sqlite3

discord.js better-sqlite3 addition is not actually adding the result from a previous const


if (command == "tgive") {
      //Get their current balance
    const grab = sql.prepare(`SELECT bal FROM ${args[1]}`).get();
    //Grab the value from the first input after the second. Ex: eco tgive 5 Juliana
    const pointsToAdd = parseInt(args[0]);
    //Add the two values from the database and the args[0] input
    const result = +grab.bal + +pointsToAdd;
    //Replace the curret value from column bal in table ${args[1]}, with the const result
    sql.prepare(`REPLACE INTO ${args[1]} (bal) VALUES ('${result}');`).run();
    message.channel.send(`You have ${grab.bal}`);

}
});

Inside of the database "Juliana", the value of column bal is 420, however, whenever I run this command with the value of 5, I get You have 420, instead of You have 425, meaning that the command did not add in the value from const result


Solution

  • const grab = sql.prepare(`SELECT bal FROM ${args[1]}`).get();
    

    When this line of code runs, it retrieves the value from the database and makes a copy of it. That means that when the database is updated, the value of grab isn't.

    If you want to get the new balance, then you'll need to query the database again.