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

Mel*_*991 5 angularjs

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

我正进入(状态 Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

这是我的代码:

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');
}]);
Run Code Online (Sandbox Code Playgroud)

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

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

'query': {method: 'GET', isArray: false }
Run Code Online (Sandbox Code Playgroud)

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

编辑:

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

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

jpa*_*7ca 16

{}之后添加'/posts'.

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

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


CLo*_*ren 6

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

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

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