Ich habe ein Artikel-Array, das ich von localStorage erhalten würde.Erstellen eines Versprechens bedingt in AngularJS
var items = JSON.parse($window.localStorage.selectedResources)['server'];
var arr = [];
var idsArray = [];
angular.forEach(items, function (item) {
idsArray.push(item.id);
});
Dann feuern ich einen Anruf API ...
//Make the API call
ds.getBillInfo(idsArray)
.then(function(response){
var serversList = [];
for (var key in response) {
// iterate over response
Das Problem ist, wenn die Elemente Array leer ist, so die idsArray
tut. Dann sagt der Fehler Cannot read property 'then' of undefined
.
Was ich tun möchte, ist even if the idsArray is empty , I want lines to execute inside the then block thinking as there is no promise
.
Wie kann ich das tun?
EDIT
Wenn ich $q.all([ds.getBillInfo(idsArray)])
tun, dann gibt es keinen Fehler.
Die getBillInfo()
wie folgt aussieht:
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'});
};
Sie müssen fix 'getBillInfo' immer ein Versprechen – SLaks
Etwas Rückkehr ist nicht hier ... Wie @SLaks sagte. – Pytth
... sogar etwas wie 'Promise.reject ('empty')' würde funktionieren, und Sie würden in den Haken kommen, oder 'Promise.resolve()', um – adeneo