Search code examples
angularjsangular-resource

Angular-resource query error


I'm receiving the following error from $resource:

Error: [$resource:badcfg] Error in resource configuration for action `query`. Expected response to contain an array but got an object

the API is not returning an Array but it return this:

{
    list: [...items...],
    next: true,
    limit: 100,
    last: 0
}

I need to get the entire object with query() and push list in my $scope.items.

The other params needs for pagination or infinite scroll.

How can I do that?

EDIT:

This is my factory:

angular.module('app').factory('Items', ['$resource',
    function ($resource) {

        return $resource('/items/:id', { id: '@id' }, {
            'query': {
                method: 'GET',
                isArray: false
            },
            update: {
                method: 'PUT'
            }
        });

    }
]);

Solution

  • Your factory is right, just remove single quotes from query:

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

    EDIT after below comment

    I had exactly the same problem few days ago, which was solved by setting isArray to true. So I quite confident in syntax, but for me I have slightly different declaration of factory:

    angular.module('docAccessApp',[]).factory('Access',function($resource){
        return  $resource('http://localhost:8080/documentAccesses/:id',{id:'@_id'},{
            query: {
                method: 'GET',
                isArray:false
            }
        });
    });
    

    Maybe you need to Hard Reload the page so that browser refreshes all the js cache.