I am trying to save an array of objects in MongoDB using node.js express and mongoose (MEAN stack). However, when I make the http post requests on the front end for each item in the array, they don't show up on the back end in that order. Here's what I have on the front end:
$scope.postThenPost = function() {
$http.post('/collection', $scope.doc).success(function(data){
for (var i = 0; i<$scope.array.length; i++) {
var req_obj = { thing: $scope.array[i] };
$http.post('/collection2', req_obj);
};
};
};
and on the back end, express runs this for posting into /collection2:
exports.create = function(req, res) {
var q = new q(req.body.thing);
q.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(q);
};
});
};
Is there something I can do to ensure that the array goes in to the database in order? Thanks in advance
$scope.arrayto/collection2, then loop through the array on back end.