【问题标题】:how to fix Expected response to contain an array but got an object ANgular js如何修复预期的响应以包含一个数组但得到一个对象 Angular js
【发布时间】:2016-07-26 07:45:23
【问题描述】:

我是新人,在调用服务后使用资源模块很难得到这个错误。 任何人都可以在我出错的地方修改我的代码错误,或者只是修改了需要纠正的部分,谢谢。

数据格式来了:-

[
    brands: Array[1]
    0: Object
    __v: 0
    _id: "5251a4a34f232fc3902"
    account_id: "525072320e32971b"
    name: "Fruits"
    __proto__: Object
    1: Object
    length: 2

    categories: Array[1]
        0: Object
        __v: 0
        _id: "5251a4a34f2323fc3902"
        account_id: "5250723230e32971b"
        name: "Fruits"
        __proto__: Object
        1: Object
        length: 2
]

错误:-

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

itmsFormView.html

<select class="form-control" ng-model="item.brand_id" id="itemBrand" >
    <option value="">Select Brand</option>
    <option ng-repeat="brand in brands" value="{{brand.brand_id}}">{{brand.name}}  </option>
</select>



 <select class="form-control" ng-model="item.category_id" id="itemCategory">           

<option value="">Select Category</option>
       <option ng-repeat="category in categories" value="{{category.brand_id}}">{{category.name}}</option>
    </select>

ItemsService.js

app.factory('itemService', function ($resource) {
    return {
        getCategoryAndBrand   :  $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
    };
});

ItemsController.js

app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
        itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){
                console.log(response);

            },function(errorResponse){
                console.log(errorResponse);
            }
        );  
});

【问题讨论】:

  • 我可能错了,我必须仔细检查文档,但是根据您的错误,资源函数是否返回一个对象并且需要一个数组?与将依赖项作为数组而不是对象传递给模块的方式相同?

标签: javascript angularjs


【解决方案1】:

来自documentation

动作是数组

isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.

根据您的代码isArray = true。将其设置为 false,您可以使用对象而不是数组。

app.factory('itemService', function ($resource) {
return {
        getCategoryAndBrand   :  $resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})
    };
});

希望您将参数作为数组而不是对象传递

来自您的代码

$resource("/user/categories_brands" ,{},{ TypeGetCategoryAndBrand:{method: 'get', isArray:true}})

如果要相信文档而不是您得到的错误,我会尝试以数组而不是对象的形式传递它,以查看您是否得到相同的响应。

【讨论】:

  • documentation 明确表示,两个可选参数(方括号所代表的)都是 Object/Hash 类型。
  • 最佳答案如何?正如@erndenson 所说,方括号代表可选而不是数组。 ://
  • 为了准确起见,我更改了它。我最初的回答是错误的,虽然它解决了这个问题,因为它需要一个数组,但仅仅是因为 isArray 操作设置为 true。
【解决方案2】:

在我的例子中,API 服务器返回一个被视为字符串的 HTML 页面(错误页面),而不是作为 javascript 对象的正确 JSON 响应。

作为旁注,javascript 错误和堆栈跟踪确实是一些东西。

【讨论】:

  • 我有类似的情况,使用 Rails 后端,我的示例应用程序默认同时提供 html 和 json,所以$resource('/posts/:id', {id: '@id'}) 给出了与上面最初提到的相同的错误。将 json 添加到 /posts 引用是一个快速修复$resource('/posts.json/:id', {id: '@id'}) 虽然当然最好只从后端提供 json
【解决方案3】:

希望这个答案还不算太晚。

您不应通过在资源声明中添加 isArray:true 语句来覆盖“get”方法的性质来解决此问题。

有两种方法可以解决此问题。

1.改用“查询”方法。它完全像“get”方法一样工作,但它的 isArray 自然设置为 true。

TypeGetCategoryAndBrand:{method: 'query'}

2.在服务器端,响应对象是一种更简单、更智能的方式。您可以继续在客户端使用“get”方法并从此更改服务器端代码:

res.jsonp(articles);

其中articles是结果数组,到这里:

res.jsonp(articles[0]);

【讨论】:

  • "改用"query"方法。它的工作方式完全像"get"方法,但它的isArray自然设置为true。" ...对我来说很合适:-)
  • JSON 编码是我的问题。谢谢
  • 救了我的命。谢谢!
【解决方案4】:

你得到一个包含两个数组的对象。

我的猜测是,在您开始使用 console.log 输出之前,您在代码中设置了 categories = response;

您需要将其设置为: categories = response.categories;

这里:

app.controller('itemsFormController', function ($rootScope, $scope, itemService, $location, $cookies, $routeParams) {
        itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}, function(response){

                categories = response.categories;
                console.log(response);

            },function(errorResponse){
                console.log(errorResponse);
            }
        );  
});

这也可能有助于理解问题:

AngularJS $resource query returns array with a function in it, which is not does not fit well when iterating through data

【讨论】:

    【解决方案5】:

    the documentation 给我的一个非常简单的解决方案:

    app.factory('Article', ['$resource', function ($resource) {
        return $resource('/v1/a/:id', null,
            {
                'query':  {method:'GET', isArray:false},
                'update': { method:'PUT' }
            });
    }]);
    

    【讨论】:

      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2015-03-11
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多