0

I am getting an array in AngularJs with factory function. This is the console

Array[0]
  0: "value1"
  1: "value2"
  length:2

But when i want to get the length of the array

console.log(array.length)

getting 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 look like:

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)
}])

I get that the length is 0. Why is that?

2
  • 2
    share more of your code. where you log in console and where you get the array? Commented Apr 3, 2017 at 9:23
  • How you are adding element to array? Kindly share. The display code of yours is proper and I think the issue is while you are adding the elements to array. Check this: stackoverflow.com/questions/36401124/… Commented Apr 3, 2017 at 9:25

4 Answers 4

1

This is a timing issue. The first time you ask for the length it is indeed 0, but when you inspect the object a few seconds later with Chrome Dev Tools you are inspecting the live object which has now been filled.

You can confirm this by using setTimeout

setTimeout(function(){
   console.log(array);
}, 0)

You can checkout this link for further explanation

UPDATE

In angular, use $timeout instead of setTimeout. Like this:

$timeout(function(){
   console.log(array);
}, 0)
Sign up to request clarification or add additional context in comments.

3 Comments

it's working. but since i am using angular i am using that with scope. I update my question.
@oded: I have updated my answer. Please let me know why the answer was downvoted
my mistaske. happen accidentley.
0

The length property of an Array in JavaScript is immutable. You can either set an Array's size by defining how many properties will be in there: var a = new Array(2), or by just passing in your values: var a = ['value1', 'value2'].

Comments

0

You mix async with sync approaches here.

Communications.find is async but you use it in getCommi like sync function.

When you call getCommi the function return immediately with empty array.

Please change according below.

app.factory("getFoo", function(Communications){
 return {
   getCommi: function(val,id, cb){
    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;
                    }
                    cb(null, array);
        });   
      }
  }
});

and

app.controller('usersFormCtrl', ['$scope','getFoo',function($scope,getFoo){
getFoo.getCommi(3,1, function(err, emails){
  $scope.formModel.emails = [];
    for(var index=0; index < $scope.emails.length; index++){
        $scope.emails = emails;
    }
});    
}])

DISCLAIMER : I don't know angular.

Comments

-1

try this,

console.log(array[0].length)

2 Comments

If length is returning 0, array[0].length will throw error. Length of undefined
can you plz add your javascript code, related to this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.