'How can I transfer all of my balance into another member? Mongoose

I am making a command that it gives all of my balance into another member, so if I use a command like ?give <member> all which is I need to find out,

if(args[1] === "all") {
        const member = await message.guild.members.fetch(args[0]) || message.mentions.members.first()
        const data = await economy.findOne({
            guildID: message.guild.id,
        })

        if(!data) return message.reply(`You don't have any records yet.`)
        if(0 === data.wallet) return message.reply(`You don't have enough balance, your current balance is $${data.wallet.toLocaleString()}`)

        economy.findOne({
            guildID: message.guild.id,
            userID: member.id,
        }, async(err, data) => {
            data.wallet += data.wallet
            data.save();
        })

        economy.findOne({
            guildID: message.guild.id,
            userID: message.author.id
        }, async(err, data) => {
            if(data) {
                const embed = new MessageEmbed()
                .setDescription(`You have successfully given ${member.user.username} amount of $${data.wallet.toLocaleString()}`)
                .setColor('RANDOM')
                .setTimestamp()
                message.channel.send({embeds: [embed]})

                data.wallet -= data.wallet
                data.save()
            }
        })

Here's what I tried so far.

This is the part that going to transfer my balance to another user:

    economy.findOne({
        guildID: message.guild.id,
        userID: member.id,
    }, async(err, data) => {
        data.wallet += data.wallet
        data.save();
    })

The thing is its not working, since its all targeting the member



Solution 1:[1]

It seems like you used the variable data for both the original user and the "targeted" user. For instance, in your second code block, instead of adding the original user's balance to data, you are adding data (of the target user) to data; basically, you are doubling the target user's data.

Try renaming your data variables to something like

    economy.findOne({
        guildID: message.guild.id,
        userID: member.id,
    }, async(err, other) => {
        other.wallet += data.wallet;
        other.save();
    })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mathletedev