Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  23] [ 5]  / answers: 1 / hits: 42802  / 11 Years ago, sat, november 16, 2013, 12:00:00

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

More From » angularjs

 Answers
9

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.


[#74250] Thursday, November 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
kourtney questions
Sun, Oct 4, 20, 00:00, 4 Years ago
Tue, Oct 29, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
Fri, Mar 1, 19, 00:00, 5 Years ago
;