noc
noc

Reputation: 1039

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object?

How fix Error:

[$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object?

// Service

   angular.module('admin.services', ['ngResource'])       
    // GET TASK LIST ACTIVITY
    .factory('getTaskService', function($resource) {
        return $resource('../rest/api.php?method=getTask&q=*',{ 'get':    {method:'GET'}});
    })

// Controller

$scope.getTask = getTaskService.query(function (response) {
    angular.forEach(response, function (item) {
        if (item.numFound > 0) {
            for(var i = 0; i < item.numFound; i++) {

                $scope.getTasks[i] = item.docs[i];

            }

        }
    });

});

Upvotes: 74

Views: 104586

Answers (6)

Adam Hughes
Adam Hughes

Reputation: 16309

For anyone coming here in 2021, another way this request may look is as follows (at least it was in our app)

angular.module('load').factory('StateService', ['$resource', 'store', function($resource, store) {
    return $resource('/some/url', {}, {
        fetchAllStateCodes: {
            method: 'GET',
            isArray: true,  // Response is an array of objects
            headers: {
                "Authorization": "Bearer " + store.get("jwt"),
                "Content-type": "application/json"
            }
        }
    });
}]);

From all of the good answers, it still wasn't obvious to me as to where to put the isArray flag...

Upvotes: 0

Karim Oukara
Karim Oukara

Reputation: 2706

$resource("../rest/api"}).get();

returns an object.

$resource("../rest/api").query();

returns an array.

You must use :

return $resource('../rest/api.php?method=getTask&q=*').query();

Upvotes: 109

Hitesh Modha
Hitesh Modha

Reputation: 2790

In order to handle arrays with the $resource service, it's suggested that you use the query method. As you can see below, the query method is built to handle arrays.

    { 'get':    {method:'GET'},
      'save':   {method:'POST'},
      'query':  {method:'GET', isArray:true},
      'remove': {method:'DELETE'},
      'delete': {method:'DELETE'} 
   };

User $resource("apiUrl").query();

Upvotes: 20

Alaa Awad
Alaa Awad

Reputation: 4152

Make sure you are sending the proper parameters too. This happened to me after switching to UI-Router.

To fix it, I changed $routeParams to use $stateParams in my controller. The main issue was that $stateParams was no longer sending a proper parameter to the resource.

Upvotes: 2

user1932958
user1932958

Reputation: 1751

Also, if your service is sending an object instead of an array add isArray:false to its declaration.

'query': {method: 'GET', isArray: false }

Upvotes: 171

Vadim
Vadim

Reputation: 8789

First of all you should configure $resource in different manner: without query params in the URL. Default query parameters may be passed as properties of the second parameter in resource(url, paramDefaults, actions). It is also to be mentioned that you configure get method of resource and using query instead.

Service

angular.module('admin.services', ['ngResource'])       
  // GET TASK LIST ACTIVITY
  .factory('getTaskService', function($resource) {
    return $resource(
      '../rest/api.php',
      { method: 'getTask', q: '*' }, // Query parameters
      {'query': { method: 'GET' }}
    );
  })

Documentation

http://docs.angularjs.org/api/ngResource.$resource

Upvotes: 34

Related Questions