所以设法解决了。发生了两件事。
首先,
Entry.create
创建对象,但由于某种原因不允许将其保存到DB。如果我将代码交换为
var entry = new ENTRY()
,然后将它们保存到数据库中。
一旦我完成了这项工作,我就遇到了一个问题,即每个条目都被多次传递到计划中(次数与要添加的条目相同,因此我最终得到了42*42个条目)。通过清除
entry
对象之前的
next()
函数被调用。我还切换到
async.eachSeries
. 我会测试的
async.each
稍后再看是否有效。
更新:
异步。每个
恢复到结果的重复,出现
异步。每个系列
需要防止这种重复。
完成最终代码以确保完整性:
// Auto-populate entries
router.post( "/populate", middlewareObj.isLoggedIn, function( req, res) {
// Ensure req.body.orgs is an array
var orgList=[]
if (typeof req.body.orgs == 'string') {
orgList = [req.body.orgs]
} else {
orgList = req.body.orgs
};
orgList.save
//lookup Plan using ID
Plan.findById( req.params.id, function(err, foundPlan){
if(err){
console.log( err);
res.redirect("/plans");
} else {
BaseData.find({
"contributingRegion": foundPlan.contributingRegion,
"org": { $in: orgList}
}, function( err, baseData) {
if (err) {
console.log( err)
} else {
// console.log( baseData.length);
//Loop through baseData to create entries & push into plan
async.eachSeries( baseData, function(data, next) {
console.log(data);
// Create new entry record
var entry = new Entry({
author: {
id: req.user._id,
username: req.user.username
},
org: data.org,
jobFamily: data.jobFamily,
globalGrade: data.globalGrade,
location: data.location,
initHC: data.initHC,
initResRate: data.initResRate.toFixed(3)
});
// Derive calculated variables for entry
entry.adjHC = entry.initHC;
entry.adjResRate = entry.initResRate;
entry.yr1Resignations = Math.round(entry.adjHC * entry.adjResRate);
entry.yr1Supply = entry.adjHC - entry.yr1Resignations;
entry.yr2Resignations = Math.round(entry.yr1Supply * entry.adjResRate);
entry.yr2Supply = entry.yr1Supply - entry.yr2Resignations;
entry.yr3Resignations = Math.round(entry.yr2Supply * entry.adjResRate);
entry.yr3Supply = entry.yr2Supply - entry.yr3Resignations;
entry.yr4Resignations = Math.round(entry.yr3Supply * entry.adjResRate);
entry.yr4Supply = entry.yr3Supply - entry.yr4Resignations;
entry.yr5Resignations = Math.round(entry.yr4Supply * entry.adjResRate);
entry.yr5Supply = entry.yr4Supply- entry.yr5Resignations;
entry.demandYr0 = entry.adjHC;
entry.demandYr1 = entry.adjHC;
entry.demandYr2 = entry.adjHC;
entry.demandYr3 = entry.adjHC;
entry.demandYr4 = entry.adjHC;
entry.demandYr5 = entry.adjHC;
entry.gapYr1 = entry.yr1Supply - entry.demandYr1;
entry.gapYr2 = entry.yr2Supply - entry.demandYr2;
entry.gapYr3 = entry.yr3Supply - entry.demandYr3;
entry.gapYr4 = entry.yr4Supply - entry.demandYr4;
entry.gapYr5 = entry.yr5Supply - entry.demandYr5;
entry.save();
// Push entry into plan
foundPlan.planEntries.push(entry);
foundPlan.save(function(err){
if (err) {
console.log(err)
} else{
entry = {}
// next iteration
next()
};
});
}, function(err) {
// This function runs when all iterations are done
if (err) throw err;
res.redirect('/plans/' + foundPlan._id);
} ); /*End of Async Loop*/
};
}); /*End of BaseDate.find*/
}
}); /*End of Plan.findById*/
});
// =====================================================
// ======UPDATE ENTRIES FROM SUPPLY/DEMAND SCREENS======
// =====================================================
// Entry Update (from Supply Screen)
router.put("/:entryId", middlewareObj.isLoggedIn, function(req, res){
// console.log(req.body)
var adjHC = req.body.entry.adjHC;
var adjResRate = req.body.entry.adjResRate / 100;
var yr1Resignations = Math.round(adjHC * adjResRate);
var yr1Supply = adjHC - yr1Resignations;
var yr2Resignations = Math.round(yr1Supply * adjResRate);
var yr2Supply = yr1Supply - yr2Resignations;
var yr3Resignations = Math.round(yr2Supply * adjResRate);
var yr3Supply = yr2Supply - yr3Resignations;
var yr4Resignations = Math.round(yr3Supply * adjResRate);
var yr4Supply = yr3Supply - yr4Resignations;
var yr5Resignations = Math.round(yr4Supply * adjResRate);
var yr5Supply = yr4Supply- yr5Resignations;
var demandYr0 = adjHC;
var demandYr1 = adjHC;
var demandYr2 = adjHC;
var demandYr3 = adjHC;
var demandYr4 = adjHC;
var demandYr5 = adjHC;
var gapYr1 = yr1Supply - demandYr1;
var gapYr2 = yr2Supply - demandYr2;
var gapYr3 = yr3Supply - demandYr3;
var gapYr4 = yr4Supply - demandYr4;
var gapYr5 = yr5Supply - demandYr5;
var updatedEntry = {adjHC : adjHC,
adjResRate : adjResRate,
yr1Resignations : yr1Resignations,
yr2Resignations : yr2Resignations,
yr3Resignations : yr3Resignations,
yr4Resignations : yr4Resignations,
yr5Resignations : yr5Resignations,
yr1Supply : yr1Supply,
yr2Supply : yr2Supply,
yr3Supply : yr3Supply,
yr4Supply : yr4Supply,
yr5Supply : yr5Supply,
demandYr0 : demandYr0,
demandYr1 : demandYr1,
demandYr2 : demandYr2,
demandYr3 : demandYr3,
demandYr4 : demandYr4,
demandYr5 : demandYr5,
gapYr1 : gapYr1,
gapYr2 : gapYr2,
gapYr3 : gapYr3,
gapYr4 : gapYr4,
gapYr5 : gapYr5 };
//lookup Entry using ID
Entry.findByIdAndUpdate(req.params.entryId, updatedEntry, {new: true}, function(err, foundEntry){
if(err){
console.log(err);
res.redirect("/plans");
} else {
res.json(foundEntry);
}
});
});