Reputation: 251
I'm making an economy system for my discord bot, some of you are here to (try to) fix the mongoose problem so you don't need to worry too much about that.
I am making a "deposit" command, which takes a certain amount of cash from your balance and puts it in your "bank".
The part where I save the amount of cash to the bank works, but the part where I subtract the amount of cash from the balance (to make it look like you took money out of your cash balance) does not work. I get this error. UnhandledPromiseRejectionWarning: CastError: Cast to Number failed for value "NaN" at path "cashBalance"
Code (that has to do with this error):
if (!isNaN(parseInt(args[0]))) {
console.log('is a num')
await depositModel.updateOne({ bankBalance: parseInt(args[0]) }).then(async () => {
await depositModel.updateOne({ cashBalance: depositModel.cashBalance - parseInt(args[0]) })
})
const embed = new Discord.MessageEmbed()
.setColor('GREEN')
.setDescription(`Successfully deposited ${parseInt(args[0])} to your bank!`)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTimestamp()
return message.channel.send(embed);
} else {
console.log('not num')
}
Upvotes: 1
Views: 292
Reputation: 23169
Any reason you're not using the $inc
operator to update your fields? You don't need all that async-await and .then()
in your code:
const amount = parseInt(args[0]);
if (isNaN(amount)) {
console.log('not num');
return;
}
await depositModel.updateOne({
$inc: {
bankBalance: amount,
cashBalance: amount * -1,
},
});
const embed = new Discord.MessageEmbed()
.setColor('GREEN')
.setDescription(`Successfully deposited ${amount} to your bank!`)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTimestamp();
return message.channel.send(embed);
Upvotes: 1