错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但得到一个对象

时间:2020-12-27 20:18:30

I am a complete noob with respect to angular, just going through a codeschool tutorial and I've hit my first hurdle.

我是一个关于角度的完整菜鸟,只是通过一个代码学教程,我已经达到了我的第一个障碍。

I am getting Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

我收到错误:[$ resource:badcfg]资源配置错误。预期的响应包含一个数组但得到一个对象

Here is my code:

这是我的代码:

var bulletinApp = angular.module('bulletinApp', ['ngResource']);

bulletinApp.controller('PostsController', ['$scope', 'Post', function($scope, Post) {
    $scope.heading = 'Welcome';
    $scope.posts = Post.query();

    $scope.newPost = {
        title: 'Test Post'
    }

}]);

bulletinApp.factory('Post', ['$resource', function($resource) {
    return $resource('/posts');
}]);

I found an answer here: Error in resource configuration while using $resource.query() function with AngularJS/Rails

我在这里找到了一个答案:在使用带有AngularJS / Rails的$ resource.query()函数时资源配置出错

That says I should add this to my resource declaration:

那说我应该把它添加到我的资源声明中:

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

Problem being I have no idea where I am supposed to add this, or even if this is the problem I'm having?

问题是我不知道我应该在哪里添加这个,或者即使这是我遇到的问题?

EDIT:

bulletinApp.factory('Post', ['$resource', function($resource) {
  return $resource('/posts', {'query': {method: 'GET', isArray: false}});
}]);

After this I am still receiving the error. Also in the tutorial, there was no need for this and I've followed it pretty much to a tee, why would the get method be getting an object instead of an array?

在此之后我仍然收到错误。同样在教程中,没有必要这样,我已经非常关注它,为什么get方法会得到一个对象而不是一个数组?

3 个解决方案

#1


16  

Add {} after '/posts'.

在'/ posts'之后添加{}。

bulletinApp.factory('Post', ['$resource', function($resource) { 
    return $resource('/posts', {}, {'query': {method: 'GET', isArray: false}}); 
}]);

For details, see this post.

有关详细信息,请参阅此帖子。

#2


6  

The result of 'Get' is array or just object? If it's an array, set isArray=true, else set isArray=false, like the guy said above

'Get'的结果是数组还是只是对象?如果它是一个数组,设置isArray = true,否则设置isArray = false,就像上面说的那样

bulletinApp.factory('Post', ['$resource', function($resource) { 
return $resource('/posts', {}, {'query': {method: 'GET', isArray: false/*true*/}}); 
}]);

And attention IsArray is different with isArray. Or if you got your data by json, please add *.json as url

并且注意IsArray与isArray不同。或者,如果您通过json获取数据,请将* .json添加为url

#3


0  

The next line of the error message in the console is a url to a fuller description of the error:

控制台中错误消息的下一行是一个更全面的错误描述的URL:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array
http://errors.angularjs.org/1.2.28/$resource/badcfg?p0=object&p1=array

According to the linked page either your server's not returning what you think it is or you need to tell $resource to expect an array from the server.

根据链接页面,您的服务器不会返回您认为的内容,或者您​​需要告诉$ resource期望服务器中的数组。

#1


16  

Add {} after '/posts'.

在'/ posts'之后添加{}。

bulletinApp.factory('Post', ['$resource', function($resource) { 
    return $resource('/posts', {}, {'query': {method: 'GET', isArray: false}}); 
}]);

For details, see this post.

有关详细信息,请参阅此帖子。

#2


6  

The result of 'Get' is array or just object? If it's an array, set isArray=true, else set isArray=false, like the guy said above

'Get'的结果是数组还是只是对象?如果它是一个数组,设置isArray = true,否则设置isArray = false,就像上面说的那样

bulletinApp.factory('Post', ['$resource', function($resource) { 
return $resource('/posts', {}, {'query': {method: 'GET', isArray: false/*true*/}}); 
}]);

And attention IsArray is different with isArray. Or if you got your data by json, please add *.json as url

并且注意IsArray与isArray不同。或者,如果您通过json获取数据,请将* .json添加为url

#3


0  

The next line of the error message in the console is a url to a fuller description of the error:

控制台中错误消息的下一行是一个更全面的错误描述的URL:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array
http://errors.angularjs.org/1.2.28/$resource/badcfg?p0=object&p1=array

According to the linked page either your server's not returning what you think it is or you need to tell $resource to expect an array from the server.

根据链接页面,您的服务器不会返回您认为的内容,或者您​​需要告诉$ resource期望服务器中的数组。