Search code examples
javascriptjsonangularjsangular-resource

$resource doesn't get array from json


I would like to get array object from server (java). Below there is angular method :

service.getAthors = function(){
        var deferred = $q.defer();

        var authors = authorResource.query(function() {
            console.log(authors);
        }).$promise.then( function(){
                deferred.resolve( "Adding book have gone correctly." );
            }, function(){
                deferred.reject("Error during adding new book.");
            });
    }

In console firebug's i see this: [{"author_id":7,"author":"Dan Brown"}] but authors array is empty. Can you tell me why ?


Solution

  • You need to assign authors to the returned server data.

    authorResource.query(function() {
            console.log(authors);
        }).$promise.then( function(data){
                authors = data;
                deferred.resolve( "Adding book have gone correctly." );
            }, function(){
                deferred.reject("Error during adding new book.");
            });