1

Well, I have a little problem. I am developing a bot, and when the user executes a daily command, he earns "money" that is added to the database. But when he adds, he ends up looking like this:

Expectation: 610 + 491 = 1101

Reality: 610 + 491 = 610491

My code:

database.ref(`Servidores/${message.guild.id}/Users/${message.author.id}/Economia`).once('value').then(async function(db){

        let dbref = database.ref(`Servidores/${message.guild.id}/Users/${message.author.id}/Economia`);
//

            const embed1 = new Discord.MessageEmbed()
            .setColor(process.env.COLOR)
            .setAuthor(` - Recompensa diária`)
            .setDescription(`Você coletou sua recompensa diária!
            
             Dinheiro Coletado: **\`R$${amount},00\`**`)
            .setTimestamp()

            message.channel.send(embed1)

            dbref.update({
                money: db.val().money+amount,
                daily: Date.now()
            })

        }
    })
Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • Does this answer your question? [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – Zsolt Meszaros Jun 01 '21 at 15:09

2 Answers2

1

When summing two variables in JavaScript, two behavior can be expected :

  • summing numbers : the result is the sum of numbers
  • summing string : the result is the concatenation of the strings.

Here one of your value, presumably db.val().money, is a string, therefore the result is the concatenation of the db.val().money and amount.

If you want the real sum, make sure to convert your variables to number with the Number() constructor.

Frédéric Lang
  • 281
  • 2
  • 11
0

because you adding strings, please convert is to integers first