node.js - 从 forEach 推送对象后数组保持为空
问题描述
需要帮助,我有问题 Array Keep Empty 从 forEach 推送对象后,我错过了什么吗?这是代码:
const allStatus = [];
result.forEach(async (element) => {
const count = await BpCandidate.count({
where: {
applicationStatusId: element.id
},
raw: true,
});
})
record = {
id: element.id,
name: element.name,
code: element.code,
description: element.description,
total: count
};
allStatus.push(record);
});
console.log(allStatus);
提前致谢
解决方案
使用for...of
对您有用,但如果您使用,可以在此处看到改进Promise.all
const allStatus = await Promise.all(result.map(async (element) => {
const count = await BpCandidate.count({
where: {
applicationStatusId: element.id
},
raw: true,
});
return {
id: element.id,
name: element.name,
code: element.code,
description: element.description,
total: count
};
}))
推荐阅读
- laravel - 如何在laravel中使用路由名称传递查询参数?
- c++ - 指向 64 位数字的 32 位是否安全?
- javascript - 使用 angular8 在循环中传递参数 onclick
- android - ARCore – 对象在面部增强中没有以正确的深度显示
- android - 如何在我的微调器中使用 getItemAtPosition 从谷歌地图中的火力基地使用 android studio
- algorithm - 在 zugzwang 的情况下需要对空移动修剪进行一些解释
- spring-webflux - 查找重复的元素,并从多个 Flux 中计数
- c# - 是否可以将 aad 注册应用程序限制为仅使用一个 Sharepoint 站点
- sql - 如何使用在 MSSQL 中动态构造的多个 LIKE 子句进行选择
- powershell - 更新/修改 ldap 用户属性 powershell