admin管理员组

文章数量:1317898

I'm getting this error "Error: [$resource:badcfg] Error in resource configuration for action 'get'. Expected response to contain an object but got an array"

and I don't know how to fix it. I have this service

angular.module('messages').factory('Messages', ['$resource',
    function ($resource) {
        return $resource('api/messages/:username', {
            username: '@username'
        });
    }]);

and this in controller:

    $scope.findOne = function () {
        $scope.messages = Messages.get({
            username: $routeParams.username
        });

        console.log($scope.messages);
    };

For this route I have in API controller this

exports.read = function (req, res) {
    res.json(req.message);
};

I know that I have to use $resource action isArray = true, but I don't know where to put it. I tried to do something like this:

angular.module('messages').factory('Messages', ['$resource',
    function ($resource) {
        return $resource('api/messages/:username', {
            username: '@username'
        }, 
            {'query': {method: 'GET', isArray: true}});
    }]);

but without result and still same error.

I'm getting this error "Error: [$resource:badcfg] Error in resource configuration for action 'get'. Expected response to contain an object but got an array"

and I don't know how to fix it. I have this service

angular.module('messages').factory('Messages', ['$resource',
    function ($resource) {
        return $resource('api/messages/:username', {
            username: '@username'
        });
    }]);

and this in controller:

    $scope.findOne = function () {
        $scope.messages = Messages.get({
            username: $routeParams.username
        });

        console.log($scope.messages);
    };

For this route I have in API controller this

exports.read = function (req, res) {
    res.json(req.message);
};

I know that I have to use $resource action isArray = true, but I don't know where to put it. I tried to do something like this:

angular.module('messages').factory('Messages', ['$resource',
    function ($resource) {
        return $resource('api/messages/:username', {
            username: '@username'
        }, 
            {'query': {method: 'GET', isArray: true}});
    }]);

but without result and still same error.

Share Improve this question asked Apr 1, 2015 at 13:46 Jaroslav KlimčíkJaroslav Klimčík 4,83813 gold badges41 silver badges60 bronze badges 2
  • 1 make isArray to false – Anita Commented Apr 1, 2015 at 13:56
  • Yep @Anita {'query': {method: 'GET', isArray: false}}); as your response is an object not an array. – rxjmx Commented Apr 1, 2015 at 13:57
Add a ment  | 

3 Answers 3

Reset to default 4

In your controller:

$scope.findOne = function () {
        $scope.messages = Messages.query({
            username: $routeParams.username
        });

        console.log($scope.messages);
    };

query Instead of get, should solve it.

Use Messages.query(...) instead of method get()

What you did is correct, but you have the use the method you just created ('query'), so the call would look like this:

$scope.findOne = function () {
    Messages.query({
        username: $routeParams.username
    }).$promise.then(function (response) {
        $scope.messages = response;
        console.log($scope.messages);
    });

};

本文标签: javascriptExpected response to contain an object but got an array for GET actionStack Overflow