【发布时间】:2021-04-30 17:23:55
【问题描述】:
我的代码有问题。我需要检查cutlist数组中是否有一个material_id在材料数据库中不存在的项目,但是forEach中的代码在函数完成后执行。
const errors: string[] = [];
await orderData.cutlist.forEach(async cutlist => {
const doesMaterialExist = await this.materialsRepository.findMaterialById(
cutlist.material_id,
);
if (!doesMaterialExist) {
errors.push('Material does not exist');
}
console.log('1')
});
console.log('2')
if (errors.length > 0) {
throw new AppError('There is a invalid material in cutlists', 404);
}
执行此代码后我的控制台是 2 -> 1。它正在检查 ForEach 循环之前的 errors.length
【问题讨论】:
标签: javascript node.js typescript typeorm