Chantal
Chantal

Reputation: 1059

Why can't I access the data within a with .query() returned $resource object?

I am trying to access the data in an object that is returned using $resource. I thought it would be similar to accessing data inside an object by using a dot but this doesn't work. In the console I can see the object contains an array named "jobs" but I can't figure out how to access this. This is the code

Service:

jenkinsDashboard.factory('Dashboard', ['$resource', function ($resource) {
    return $resource(
        'http://ci.angularjs.org/view/AngularJS/api/json?pretty=true',
        {}, 
        { query: { method:'GET', params:{}, isArray:false }});
    }]);

Controller:

dashboardControllers.controller('DashboardCtrl', ['$scope', 'Dashboard',
function($scope, Dashboard) {
    $scope.allData = Dashboard.query();
    console.log($scope.allData.jobs);
    console.log($scope.allData);
}]);

Output:

undefined                                                                  
Resource {$promise: Object, $resolved: false, $get: function, $save: function, $query: function…}

Upvotes: 2

Views: 898

Answers (1)

Simba
Simba

Reputation: 11

I had the same issue using $resources, find below a restructuring of your controller

    dashboardControllers.controller('DashboardCtrl', ['$scope', 'Dashboard',
 function($scope, Dashboard) {

Dashboard.query().$promise.then(function (res) {
        $scope.allData = res;
        console.log($scope.allData.jobs);
        console.log($scope.allData);
    });
 }]);

Upvotes: 1

Related Questions