0

起初我有这个,但得到了错误Expected response to contain an object but got an array

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
});
}]);

所以我改成这样:

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
}, {'save': {method: 'POST', isArray:true}}); // this line is added
}]);

我将一个项目保存到数据库,并返回对象列表作为结果。返回一个 JSON 数组,但我收到此错误。

有什么建议么?

4

1 回答 1

1

isArray属性适用于请求和响应。请参阅AngularJs $resource 对 isArray 的 request 和 response 使用

由于请求不是数组,因此出现错误。您可以将其包装在一个数组中,然后将其拉出服务器端。

于 2013-11-25T20:16:09.793 回答