错误:[$ resource:badcfg]资源配置错误.预期的响应包含一个数组但有一个对象 [英] Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

查看:306
本文介绍了错误:[$ resource:badcfg]资源配置错误.预期的响应包含一个数组但有一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就角度而言,我是一个完全的菜鸟,只是参加了一个代码学校的教程,而我遇到了第一个障碍.

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

我得到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');
}]);

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

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

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

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?

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

此后,我仍然收到错误.同样在本教程中,也不需要这样做,而我在T恤上已经非常关注它了,为什么get方法将获取对象而不是数组?

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?

推荐答案

'/posts'之后添加{}.

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

有关详细信息,请参见这篇文章.

For details, see this post.

这篇关于错误:[$ resource:badcfg]资源配置错误.预期的响应包含一个数组但有一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆