I am trying to query, that would get all of the earning and withdrawal amount and sum, but somehow after the forEach loop is done and exits, all of the updated values I placed in a variable return to their original values.
var withdrawbalance = 0;
var totalearning = 0;
userplans.forEach((up) => {
DepositEarning.findOne({deposit: up.deposit._id})
.then(depositearning => {
withdrawbalance += parseInt(depositearning.WithdrawableBalance, 10);
});
Earning.findOne({deposit: up.deposit._id})
.then(earnings => {
totalearning += parseInt(earnings.Earning, 10);
});
})
console.log(withdrawbalance);
console.log(totalearning);