I need to design a function in javascript that makes requests to POST multiple items to the database. I then have to take all the data that has been returned and run another $http query to the database.
This is my code as it stands
var current_request=null;
var workorders=[];
$scope.submit_work_order=function(){
for(var i=0;i<$scope.parent_jobs.length;i++){
addPartAndWorkOrder(equipment_id,part_number,work_order,_id);
}
if(current_request!=null){
current_request.then(function(){
if(workorders.length>0){
$http({
url:'/api/salesorders',
method: "POST",
data: {'workorders':workorders}
})
}
});
}
function addPartAndWorkOrder(equipment_id,part_serial_number,work_order_id, job_id){
//part_serial_number
var info;
info=$http({url:'/api/parts',
method: "POST",
data: {'equipment':equipment_id});
if(current_request==null){
current_request=info;
}
else{
current_request.then(function(){
return info
});
});
}
current_request.then(function(data){
return $http({
url:'/api/workorders',
method: "POST",
data: {workorder_id:work_order_id, part:data._id,job:job_id}
}).then(function(data){
workorders.push(data._id);
});
})
}
What I need is to populate the array workorders by running the HTTP requests in the code.
Then I need to run the final run another request to the endpoint api/salesorders/ with the data from the array
TL;DR
This is the way the endpoints should run
parts->workorders->parts->workorders->salesorder
This is the way they do run
parts->salesorder->workorders->parts->workorders
I tried to keep adding to the .then but as it currently stands its still failing
$q.all()see $q docs. Basically would allow the first group to all complete before sending the next group