2

I have an array of objects called user. So when I do console.log(user[0]); I get an output like : Object {activityId: "2", id: "1", activityDt: "01/15/2016"}

So then I decided to store the user[0] in a seprate object.

var p = user[0];

Now finally I want to get the activityId of the object so I do

console.log(p.activityId);

But nothing gets printed and I get an error. Please suggest.

My code

 mainFactory.getUser()
  .success(function(usersData) {
      $scope.userData = usersData;

      // Determine which events we will show (removce certain events)

      var userActivity = [];
      angular.forEach($scope.userData, function (user, index){

        // console.log(user.length);

        //for(var i = 0; i<user.length; i++){
        //   console.log(user[i]);
        // }

        // console.log(user[0]);
        var p = user[0];
        console.log(p.activityId);

        // for (var key in p) {
        //   alert(p[key]);
        // }




      });
    })
    .error(function(err) {
      console.log('Error: ' + err);
    });

Error

TypeError: Cannot read property 'activityId' of undefined
    at main.controller.js:54
    at Object.forEach (angular.js:334)
    at main.controller.js:44
    at angular.js:9433
    at processQueue (angular.js:13318)
    at angular.js:13334
    at Scope.$eval (angular.js:14570)
    at Scope.$digest (angular.js:14386)
    at Scope.$apply (angular.js:14675)
    at done (angular.js:9725)
15
  • 1
    What do you get if you try console.log(p)? Commented Feb 27, 2016 at 18:32
  • 1
    Post more code, because that should work if things really are exactly as you describe. Are those lines of code one after the other in the program, or are they separated somehow? Commented Feb 27, 2016 at 18:33
  • @JanTojnar if i do console.log(p); i get same result as console.log(user[0]); Commented Feb 27, 2016 at 18:34
  • 1
    I've run those lines of code myself, and they do indeed work. We will need to see much more details of the code to help with this. Posting the error message would also be helpful. Commented Feb 27, 2016 at 18:35
  • 1
    inside angular.forEach, u are using var p = user[0];, it should be var p = user; Commented Feb 27, 2016 at 18:44

1 Answer 1

2

once you have used forEach, you are getting individual elements of $scope.userData. Calling users as though an array will not work and user[0] will be undefined, as reported by your error

angular.forEach($scope.userData, function (user, index){
    var p = user;
    console.log(p.activityId);
Sign up to request clarification or add additional context in comments.

5 Comments

Just a question though, basically i want to print id or say activityId of each individual how do i do that because doing var p = user is storing all the objects i think.
I just tried that -> console.log(p.activityId); i get undefined.
you mean do console.log(user) ?
[Object, Object, Object] is what i get when i do console.log(user)
So you assert console.log(user[0]); gives Object {activityId: "2", id: "1", activityDt: "01/15/2016"} inside the forEach loop?

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.