Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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];

            }

        }
    });

});
like image 230
noc Avatar asked Nov 18 '13 06:11

noc


2 Answers

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

'query': {method: 'GET', isArray: false }
like image 119
user1932958 Avatar answered Nov 16 '22 10:11

user1932958


$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();
like image 107
Karim Oukara Avatar answered Nov 16 '22 10:11

Karim Oukara