Nodejs:按顺序递增foreach循环内的序列号

时间:2017-06-03 06:11:37

标签: node.js mongodb async.js

我一直致力于MEAN堆栈项目,目前还没有使用Angular。我被困在一个地方,我尝试了很多东西摆脱,但没有选择工作。我一直在使用偶发射器来处理Node.js的异步性质,以便顺序执行逻辑验证。我的代码是

            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,现在是时候将记录插入数据库了。我的情况就像我吃了整只大象而只留下了尾巴。那尾巴越来越难以消化......

任何关于如何迎合它的建议都会非常感激......

0 个答案:

没有答案