My scenario involves retrieving an items array from localStorage.
var items = JSON.parse($window.localStorage.selectedResources)['server'];
var arr = [];
var idsArray = [];
angular.forEach(items, function (item) {
idsArray.push(item.id);
});
After collecting the item IDs, I initiate an API call...
//Make the API call
ds.getBillInfo(idsArray)
.then(function(response){
var serversList = [];
for (var key in response) {
// iterate over response
The issue arises when the items array is empty, causing idsArray
to also be empty. This results in an error stating
Cannot read property 'then' of undefined
.
I aim to execute the lines inside the then block even if idsArray
is empty as if there is no promise involved.
EDIT
When using
$q.all([ds.getBillInfo(idsArray)])
, the error disappears.
The definition of getBillInfo()
function is as follows:
this.getBillInfo = function(idsArray){
if(!idsArray.length) return;
var segmentUrl = '';
for(var i =0;i<idsArray.length;i++){
if(i != (idsArray.length-1))
segmentUrl += 'ids='+idsArray[i]+'&';
else
segmentUrl += 'ids='+idsArray[i];
}
return HttpWrapper.send('/api/bill?bill=t&'+segmentUrl, {"operation": 'GET'});
};