Struggling with sending a post request in Angular using angular.forEach loop and resolving promise with $q.all()...
This is my current setup:
angular.module('dataApp')
.factory('UserService', function () {
var User = $resource(ENV.API_URL + '/user/:email', {
email: '@email'
}, {
add: {
method: 'POST',
url: ENV.API_URL + '/user/:email/medication/:medicationId',
params: {
email: '@email',
dataId: '@medicationId'
}
}
});
User.addData = function (dataList) {
var promiseArray = [];
angular.forEach(dataList, function (data) {
User.add({
email: currentUser.email,
dataId: data._id
}).$promise.then(function success(response) {
promiseArray.push(response.$promise);
}, function error(response) {
promiseArray.push(response.$promise);
promiseArray.push(response.$promise);
});
});
return $q.all(promiseArray);
};
});
Encountering an issue where the promises pushed into promiseArray turn out to be empty quotes (e.g. console.log(promiseArray) ==> ["", ""]). Any idea what might be missing here?