Within my code, I am working with an array of objects called user. When I try to log the first object using console.log(user[0]);
, I receive this output:
Object {activityId: "2", id: "1", activityDt: "01/15/2016"}
Afterwards, I attempt to store user[0] in a separate object.
var p = user[0];
Subsequently, when I aim to retrieve the activityId property from the object, I use:
console.log(p.activityId);
Unexpectedly, nothing is being printed and an error occurs. Any suggestions would be greatly appreciated.
Snippet of my code:
mainFactory.getUser()
.success(function(usersData) {
$scope.userData = usersData;
// Determine which events we will show (remove 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 Message:
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)