Having a slight issue, I am trying to return an array using a function in Angular. I am processing my data within an anonymous function and when I try to return the array, it ends up empty.
I'm not sure what to do...
getCurrentExchangeTo : function(year, month, country){
var numberDayPerMonth = [
31,
28,
31,
30,
31,
30,
31,
30,
31,
30,
31,
30,
];
var vm = this;
this.country = country;
this.getCurrentExchangeFor = [];
var hello = "gello"
for(var i = 0; i < numberDayPerMonth.length; i++){
if((i + 1) === month){
for(let j = 1; j < numberDayPerMonth[i]; j++){
$http.get('http://api.fixer.io/' + year + '-0' + month + '-0' + j +'?symbols=' + vm.country).then(function (success) {
let countryDay = vm.country
vm.getCurrentExchangeFor[j] = success.data.rates[countryDay];
});
}
}
}
return vm.getCurrentExchangeFor
}
thanks
EDIT Using promise but it's only returning one piece of data
getCurrentExchangeTo : function(year, month, country){
var def = $q.defer();
var numberDayPerMonth = [
31,
28,
31,
30,
31,
30,
31,
30,
31,
30,
31,
30,
];
var vm = this;
this.country = country;
this.getCurrentExchangeFor = [];
var hello = "gello"
for(var i = 0; i < numberDayPerMonth.length; i++){
if((i + 1) === month){
for(let j = 1; j < numberDayPerMonth[i]; j++){
$http.get('http://api.fixer.io/' + year + '-0' + month + '-0' + j +'?symbols=' + vm.country).then(function (success) {
let countryDay = vm.country
vm.getCurrentExchangeFor[j] = success.data.rates[countryDay];
def.resolve(success.data.rates[countryDay])
});
}
}
}
return def.promise
}