1 回答
TA贡献1848条经验 获得超2个赞
我看urcode,似乎你没有等待承诺来解决
const promises = emp_id.map((assignee) => {
return new Promise(function (resolve, reject) {
Employee.findById(assignee, function (err, employee) {
if (err) {
console.log("error in finding employee :" + assignee);
reject(
"There was an error loading the employee data in to the database : " +
err.message
);
} else {
tasklist.taskAssignee.push(employee);
console.log("employee saved");
resolve("Employee records uploaded successfully");
}
});
});
});
await Promise.all(promises) // here wait..
整个示例。
router.post("/tasklist/:id/edit", function (req, res) {
Tasklist.findOneAndUpdate(
{ _id: req.params.id },
req.body.tasklist,
async function (err, tasklist) {
if (err) {
req.flash(
"error",
"Unable to update the tasklist record : " + err.message
);
res.redirect("/employee");
} else {
var emp_id = req.body.taskAssignee.split(",");
try {
const promises = emp_id.map((assignee) => {
return new Promise(function (resolve, reject) {
Employee.findById(assignee, function (err, employee) {
if (err) {
console.log("error in finding employee :" + assignee);
reject(
"There was an error loading the employee data in to the database : " +
err.message
);
} else {
tasklist.taskAssignee.push(employee);
console.log("employee saved");
resolve("Employee records uploaded successfully");
}
});
});
});
await Promise.all(promises)
console.log("Tasklist Saving....");
let savetasklist = await tasklist.save();
console.log("Tasklist Saved : ");
req.flash("success", "Updated Tasklist" + req.body.tasklist);
res.redirect("/tasklist");
} catch (err) {
req.flash("error", "Updating Tasklist" + err);
res.redirect("/tasklist");
}
}
}
);
});
添加回答
举报