-2

I am making a balance system, and I created a give command. Whenever I give someone some money ($10 for example), their balance shows as "$10 [object Promise]". How can I remove "[object Promise]"?

Balance: $100 [object Promise]

Full command code:

        if (command === 'give') {

            if (!args[0]) return message.channel.send('how much are you gonna send?')

            if (isNaN(args[0])) return message.channel.send('format: xd give [amount] [@user]')

            const money = args[0];

            const emb = new Discord.MessageEmbed()
            .setColor('GRAY')
            .setTitle('you gave them the $$')
            .setDescription(`you gave ${message.mentions.users.first().username} the $$$$$$ (` + args[0] + ` to be exact)`)
            .setTimestamp()
            .setFooter('sent to pokimane');

            message.channel.send(emb);

            const money2 = moneyAmount - args[0];

            if (isNaN(money2)) {
                Number(money2);
            }

            moneydb.set(message.author.id, money2);



                        const uMA = moneydb.get(message.mentions.users.first().id);

                        const money3 = args[0] + uMA;

                        moneydb.set(message.mentions.users.first().id, money3);
        }
// keyv connection
const moneydb = new Keyv('mysql://[user and pass]@localhost:3306/money');

Even the database has [object promise] at the end

phpMyAdmin screenshot

ethry
  • 731
  • 6
  • 17
  • `[object Promise]` means you're trying to send an object of a promise – Elitezen May 28 '21 at 23:32
  • You'd have to show us the code where that promise comes from before we could offer ideas for how to deal with the promise correctly. – jfriend00 May 28 '21 at 23:51
  • @jfriend00 There are no promises though... – ethry May 29 '21 at 00:41
  • Well, apparently none that you're aware of, but clearly there is a promise that somebody tried to turn into a string and that's where the `[object Promise]` comes from. Can you show ALL the code that is involved in calculating or displaying the balance because somewhere along that line of execution, there was a promise involved. – jfriend00 May 29 '21 at 00:44
  • @jfriend00 I updated it with more information – ethry May 29 '21 at 00:55
  • My guess is that `moneydb.get(message.mentions.users.first().id);` returns a promise. What database is `moneydb`? And, you create the funny string with `[object Promise]` in it here: `const money3 = args[0] + uMA;`. – jfriend00 May 29 '21 at 00:57
  • The moneydb database is what stores the amount of money the user has. It is a number so it doesn't make a string (I tested it on other commands) – ethry May 29 '21 at 01:00

1 Answers1

1

If moneydb is a Keyv database as it appears in your question, then this line of code:

const uMA = moneydb.get(message.mentions.users.first().id);

puts a promise into uMA. You are apparently missing an await like this:

const uMA = await moneydb.get(message.mentions.users.first().id);

So, when you do this:

 const money3 = args[0] + uMA;

And uMA is a promise, it tries to make them both into a string and you get the output that contains [object Promise].

FYI, you can see from the Keyv documentation here that both .set() and .get() return promises.

jfriend00
  • 683,504
  • 96
  • 985
  • 979