Currently, I am attempting to send an array of promises to an express app in order to retrieve data from a mongo database. The behavior seems to be working fine on the front end. In this scenario, both objects are sent to the server and resolved using $q.all. However, during my debugging process on the server side, I noticed that both objects end up being the same. Specifically, it is the last promise with payload2 that gets resolved twice. Even when adding more promises, the correct number gets resolved but they all seem to be values from the last object in the array.
var promises = logCspItemInventoryStatus(payload,result.cspItems);
return $q.all(promises)
.then(function(result){
toastr.success('Items have been logged');
})
.catch(function(err){
toastr.error(err);
})
var logCspItemInventoryStatus = function (payload,cspItems) {
var promises = [];
angular.forEach(cspItems, function(item){
//I am appending payload with items
payload.qty=item.checking;
payload.description=item.description;
//payload 1 {docId: "55c124f7485684e81d6181fc", by: "Foo Bar", checkIn: false, qty: 2, description: "car"}
//payload 2 {docId: "55c124f7485684e81d6181fc", by: "Foo2 Bar2", checkIn: false, qty: 1, description: "hall"}
var p = checkOutCspItem(payload);
promises.push(p);
});
return promises;
};
//returns a promise
var checkOutData = function(payload) {
return Csp.update(payload).$promise.then(function(result) {
return result.data
});
}