const users = [];
directSponsers.forEach(async (user) => {
if (user.is_calculated.ds === true) return;
const updatedUser = await User.updateOne(
{ _id: user._id },
{ $set: { "is_calculated.ds": true } }
);
const pack = packages.find(
(p) => String(p._id) === String(user.packages[0].packageId)
);
totalCommission += pack.sponserPrice * pack.commission;
const userCommission = pack.sponserPrice * pack.commission;
users.push({
_id: user._id,
name: user.name,
email: user.email,
total_amount: pack.sponserPrice,
commission: userCommission,
direction: user.leg,
});
});
if (users.length === 0)
return res
.status(400)
.json({ message: "No children to get direct commission from" });
This is my code, the call to User.updateOne()
just throws me out of forEach
loop to the if condition at the end and users.length
is equal to zero. If I remove that User.updateOne()
it works just fine.