javascript - async await 让我脱离了 forEach 循环 javascript
问题描述
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" });
这是我的代码,对的调用只是将我从循环中User.updateOne()
抛出到最后的 if 条件并且等于零。如果我删除它就可以了。forEach
users.length
User.updateOne()
解决方案
推荐阅读
- python - 学习 asyncio:异步运行一个有延迟的函数
- angular - 使用 @Storybook/addon-docs 显示模板的代码
- django - Django:如何将两个序列化器相互嵌套?
- javascript - 搜索栏返回所有列的结果
- python - 如何在 django/boostrap 中按类别过滤任务?
- python - 使用 openCV 从 numpy 数组生成视频
- laravel - Salesforce JWT 登录返回无效的客户凭证
- php - Jquery ajax表单提交即使返回false?
- swift - 测验结束后如何自动切换到另一个 viewController (SWIFT)
- spring-boot-chaos-monkey - 无法将“chaos.monkey.assaults”下的属性绑定到 de.codecentric.spring.boot.chaos.monkey.configuration.AssaultProperties