gpt4 book ai didi

angularjs - 错误 : [$resource:badcfg] Error in resource configuration. 预期响应包含一个数组但得到一个对象

转载 作者:行者123 更新时间:2023-12-04 16:23:15 24 4
gpt4 key购买 nike

我是一个完整的 angular 菜鸟,只是通过了一个代码学校教程,我已经遇到了我的第一个障碍。

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

我在这里找到了答案:
Error in resource configuration while using $resource.query() function with AngularJS/Rails

那就是说我应该将它添加到我的资源声明中:
'query': {method: 'GET', isArray: false }

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

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

在此之后,我仍然收到错误。同样在本教程中,没有必要这样做,我几乎遵循了它,为什么 get 方法会获取对象而不是数组?

最佳答案

添加 {}之后 '/posts' .

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

详情见 this post .

关于angularjs - 错误 : [$resource:badcfg] Error in resource configuration. 预期响应包含一个数组但得到一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20321351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com