Dennis
Dennis

Reputation: 368

How can I console log this object

Maby it just oversee it but I can't console log the value

So when I log this

console.log($scope.data)

Than I get back in the console on the page

$$state: Object
status: 1
    value: Array[11]
    0: Object
    1: Object
    2: Object
    3: Object
    4: Object
    5: Object
    6: Object
    7: Object
    8: Object
    9: Object
    10: Object

I would just say that

console.log($scope.data.value[0]) 

Gives me just the first object in the value. But I only get undefined with whatever I try to use. What do I miss that I can't just get my value of the object separately. Maby it is a angular thing because of the $$ but im kinda clueless right now.

Here is a screenshot of what I get when I console log the data

update for posting my code how I get my data:

Factory

(function () {
'use strict';
angular
    .module('testApp')
    .factory('storage', storage);

storage.$inject = ['$http'];

function storage($http) {
    return {
        getData: getData
    };

    function getData() {
        return $http.get('http://localhost:4040/')
            .then(complete)
            .catch(failed);

        function complete(response) {
            var merge = data.teams.map (function (data) {
                data.members = response.data.filter (function (person) {
                    return person.room == data.rooms
                });
                return data;
            });
            return merge;
        }

        function failed(error) {
            console.log('getting the data has failed' + error.data);
        }
    }
}
})();

service

(function () {
'use strict';
angular
    .module('testApp')
    .service('testStorage', testStorage);

testStorage.$inject = ['storage'];

function testStorage(storage) {
    //Create empty variables


    //Data in variables
    var allData = storage.getData();

    //Return functions
    var service = {
        getAll: getAll
    };

    return service;

    ///////////////////////////
    // Create functions here //
    ///////////////////////////

    function getAll() {
        return allData;
    }

}
})();

and final my controller

(function() {
'use strict';

angular
    .module('testApp')
    .controller('tryOutController', tryOutController);

tryOutController.$inject = ['$scope', 'testStorage'];

function tryOutController($scope, testStorage) {
    $scope.data = {};
    $scope.data = testStorage.getAll();

    console.log($scope.data);

}
})();

Upvotes: 1

Views: 143

Answers (3)

Katana24
Katana24

Reputation: 8959

The code below will "stringify" (convert to a string representation) any object that is passed to it. So, in your case you have an array of objects so this will print the first one in that array.

console.log(JSON.stringify($scope.data.value[0]));

You could go further and use a for loop to iterate over the array and print each in turn:

for(var i = 0; i < $scope.data.value.length; i++) {
   console.log(JSON.stringify($scope.data.value[0]));
   // For spacing...
   console.log('---------------------');
}

Upvotes: -2

dfsq
dfsq

Reputation: 193311

So the problem is that you are trying to access your data before it's populated. Use proper promises capabilities:

function tryOutController($scope, testStorage) {
    $scope.data = {};
    testStorage.getAll().then(function(data) {
        $scope.data = data;
        console.log($scope.data);
    });
}

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1075427

It sounds to me like you're logging the object before it has the values on it. The console, on many browsers (like Chrome), logs a reference to the object which the console uses later if you expand it. This means that when you expand the object in the console later, you see the values as they are then, not as they were when you logged the object.

So if console.log($scope.data.value[0]) is showing you undefined but console.log($scope.data) shows you a value array which, when you expand it, has values it it, that means it got filled in after it was logged.

But: Looking at your first example, I think you should be looking at $scope.data.$$state.value[0], not $scope.data.value[0].

Upvotes: 2

Related Questions