在下面的代码中,我正在等待猫鼬的数据库调用解决。即使这样,最终声明
res.json({err: null,data: studentArray})
在上述循环完成之前执行。
这是该功能的完整代码。
router.post("/",moderator,(req,res) => {
const { filters } = req.body;
const { students } = filters;
const studentArray = [];
try {
if (students.branches) {
students.branches.forEach(async branch => {
const departments = await Department.find({ name: branch });
departments.forEach(async dept => {
const studentPerBranch = await Student.find({ branch: dept.id });
// console.log(studentPerBranch);
studentArray.push(studentPerBranch);
});
});
res.json({ err: null,data: studentArray });
} else res.status(404).json({ err: "Branches not found" });
} catch (err) {
res.status(500).json({ err,data: null });
}
});