In AngularJs, I am retrieving an array using a factory function. This is the console output:
Array[0]
0: "value1"
1: "value2"
length:2
However, when I try to get the length of the array:
console.log(array.length)
I am fetching data from MySQL in Loopback.
app.factory("getFoo", function(Communications){
return {
getCommi: function(val,id){
var array = [];
var myVals = Communications.find({
filter: {
where: {
and : [{
communications_type_code : val
},{
object_id : id
}]
}
}
}, function(data){
for(var i=0; i< data.length; i++){
array[i] = data[i].contact_value;
}
return array;
});
return array;
}
}
});
The controller code looks like this:
app.controller('usersFormCtrl', ['$scope','getFoo',function($scope,getFoo){
var emails = getFoo.getCommi(3,1);
setTimeout(function(){
$scope.formModel.emails = [];
for(var index=0; index < $scope.emails.length; index++){
$scope.emails = emails;
}
}, 0)
}])
Even though it seems like there should be data in the array, the length is showing as 0. Why is that happening?