Node.js Nodejs:按顺序递增foreach循环内的序列号
我一直在做一个MEAN stack项目,现在还没有使用Angular。我被困在一个点上,我尝试了这么多的事情来摆脱,但没有选择的工作。我一直在使用偶数发射器来处理Node.js的异步特性,以便按顺序执行逻辑验证。我的代码是Node.js Nodejs:按顺序递增foreach循环内的序列号,node.js,mongodb,async.js,Node.js,Mongodb,Async.js,我一直在做一个MEAN stack项目,现在还没有使用Angular。我被困在一个点上,我尝试了这么多的事情来摆脱,但没有选择的工作。我一直在使用偶数发射器来处理Node.js的异步特性,以便按顺序执行逻辑验证。我的代码是 flowController.on('12', function (combinedArray, finalPickSubListInsertArray) { var dummyObject = [];
flowController.on('12', function (combinedArray, finalPickSubListInsertArray) {
var dummyObject = [];
async.forEach(finalPickSubListInsertArray, function (element, callback) {
locationStoreModel.findOne({'_id': element.locationStoreId, 'activeStatus': 1}, function (err, locationRow) {
if (err) {
flowController.emit('ERROR', {message: "Location data missing! Records tampered/removed from system", status: 'error', statusCode: '404'});
} else {
pickSubListModel.find({'pickListId': pickListId, 'activeStatus': 1}).sort({'sequence': -1}).exec(function (err, pickSubListRow) {
if (err) {
res.json({message: 'Internal Error fetching pick sublists!', status: 'error', statusCode: '500'});
} else {
var sequence = (pickSubListRow.length == 0) ? 1 : (pickSubListRow[0].sequence + (dummyObject.length + 1));
var newPickSubList = new pickSubListModel();
newPickSubList.pickListId = pickListId;
newPickSubList.itemCode = itemCode;
newPickSubList.itemStoreId = element.itemStore;
newPickSubList.itemDescription = itemDescription;
newPickSubList.requiredQuantity = requiredQuantity;
newPickSubList.sequence = sequence;
newPickSubList.createdBy = createdBy;
newPickSubList.timeCreated = timeInInteger;
newPickSubList.save(function (err, insertedRecordDetails) {
if (err) {
res.json({message: 'INTERNAL SERVER ERROR, UNKNOWN SERVER ERROR HAS OCCURRED', status: 'error', statusCode: '500'});
} else {
pickListModel.update(
{'_id': pickListId},
{'$addToSet': {'pickSubLists': insertedRecordDetails._id.toString()}},
function (err) {
if (err) {
// error while adding records
res.json({message: "Unable to make update! Try again after some time.", status: 'error', statusCode: '500'});
} else {
dummyObject.push({process: 'done'});
callback();
}
});
}
});
}
});
}
});
}, function (err) {
if (err) {
res.json({message: 'Unable to create pick-sublist! Try again later.', status: 'error', statusCode: '304'});
} else {
res.json({message: 'New Pick-Sublist added into the system!', status: 'success', statusCode: '200'});
}
});
});
在这里,我尝试将连续记录插入MongoDB集合,插入的每条记录都将获得递增的序列号我的问题是,当我进入foreach时,记录会被插入到集合中,但所有记录都会以增量方式获得相同的序列号。
我做了一系列的11次验证,然后到达事件发射器12,现在是将记录插入数据库的时候了。我的处境就像我吃掉了整头大象,只剩下尾巴&那条尾巴让我难以消化
如果您对如何满足这一需求有任何建议,我们将不胜感激……您是否尝试了
async.eachSeries
使其自然同步?不,亲爱的……好的,让我看看它是多么神奇!它就像一个符咒…谢谢