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
3 Answers
Reset to default 4In 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
版权声明:本文标题:javascript - Expected response to contain an object but got an array for GET action - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034336a2417052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论