如何修复预期响应包含一个数组,但得到的对象角的js [英] how to fix Expected response to contain an array but got an object ANgular js

查看:224
本文介绍了如何修复预期响应包含一个数组,但得到的对象角的js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的之一,所以很难得到利用资源模块调用服务后,这个错误。
任何一个可以修改我的错误在code,其中我得到错误的或者仅仅是修改了其中pieace需要纠正感谢名单必须是AP preciated。

格式的数据传来: -

  [
    品牌:数组[1]
    0:对象
    __v:0
    _id:5251a4a34f232fc3902
    ACCOUNT_ID:525072320e32971b
    名称:水果
    __proto__:对象
    1:对象
    长度:2    类别:数组[1]
        0:对象
        __v:0
        _id:5251a4a34f2323fc3902
        ACCOUNT_ID:5250723230e32971b
        名称:水果
        __proto__:对象
        1:对象
        长度:2
]

错误: -

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

itmsFormView.html

 <选择类=表格控NG模型=item.brand_idID =itemBrand>
    <期权价值=>选择品牌< /选项>
    <选项NG重复=品牌在品牌VALUE ={{brand.brand_id}}> {{brand.name}}< /选项>
< /选择> <选择类=表格控NG模型=item.category_idID =itemCategory><期权价值=>选择类别< /选项>
       <选项NG重复=类别中的大类VALUE ={{category.brand_id}}> {{category.name}}< /选项>
    < /选择>

ItemsService.js

  app.factory('itemService',函数($资源){
    返回{
        getCategoryAndBrand:$资源(/用户/ categories_brands,{},{TypeGetCategoryAndBrand:{方法:'得到',IsArray的:真正}})
    };
});

ItemsController.js

  app.controller('itemsFormController',函数($ rootScope,$范围,itemService,$位置$饼干,$ routeParams){
        itemService.getCategoryAndBrand.TypeGetCategoryAndBrand({}功能(响应){
                的console.log(响应);            },功能(errorResponse){
                的console.log(errorResponse);
            }
        );
});


解决方案

文档

动作IsArray的

IsArray的 - {布尔=} - 如果为true,则此操作返回的对象是一个数组,请返回栏目

根据您的code IsArray的= TRUE。它设置为false,你可以使用一个对象,而不是一个数组。

  app.factory('itemService',函数($资源){
返回{
        getCategoryAndBrand:$资源(/用户/ categories_brands,{},{TypeGetCategoryAndBrand:{方法:'得到',IsArray的:真正}})
    };
});

该公司预计,你在参数传递作为数组不是对象

从code

$资源(/用户/ categories_brands,{},{TypeGetCategoryAndBrand:{方法:'得到',IsArray的:真正}})

如果该文档是比你得到我会尝试通过在这个数组的形式,而不是一个对象,看看你得到同样的响应的错误才能相信。

i am new one so difficult to getting this error using resource module after calling service. can any one modify my mistake in code where i am getting wrong or just modified that pieace of which needs to rectify thanx must be appreciated.

Format Of data Coming:-

[
    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
]

Error:-

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

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);
            }
        );  
});

解决方案

From the documentation:

Action isArray

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

According to your code isArray = true. Set it to false and you can use an object instead of an array.

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

It's expecting you to pass in the parameters as an array not an object

from your code

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

If the documentation is to be believed than with the error your getting I'd try to pass this in the form of an array and not an object to see if you get the same response.

这篇关于如何修复预期响应包含一个数组,但得到的对象角的js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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