Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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);
            }
        );  
});
share|improve this question
    
I could be wrong about this, I have to double check the documentation, but based on your error, could it be that the resource function returns an object and an array is expected? The same way that you pass dependencies to a module as an array and not an object? – richbai90 Nov 16 '13 at 15:25
up vote 15 down vote accepted

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.

share|improve this answer
1  
the documentation explicitly says, that both optional parameters (what the square brackets stand for) are of type Object/ Hash. – ernd enson Jan 2 '14 at 13:37
1  
How is this the best answer? As @erndenson notes, square brackets stand for optional and not array. :/ – Apoorv Parijat Feb 8 '14 at 13:23
    
I changed this for accuracy. My original response was wrong, though it fixed the issue because it was expecting an array, but only because the isArray action was set to true. – richbai90 Feb 10 '14 at 16:37

In my case, the API server was returning an HTML page (an error page) that is treated as a string, instead of the correct JSON response which would be a javascript object.

As a sidenote, javascript errors and stacktraces are really something.

share|improve this answer
    
I had a similar situation, using Rails backend, my example app was serving both html and json by default so $resource('/posts/:id', {id: '@id'}) gave the same error as initially mentioned above. Adding json to the /posts reference was a quick fix $resource('/posts.json/:id', {id: '@id'}) although of course it would be better to serve json only from the backend – Jonathon Batson Jun 24 '14 at 0:50

Hope this answer is not too late.

You should not fix this problem by putting a isArray:true statement to override "get" method's nature in your resource declaration.

There are two ways to fix this problem.

1.Use "query" method instead. It works like "get" method totally, but its isArray is set to true naturally.

TypeGetCategoryAndBrand:{method: 'query'}

2.On the server side, responding an object is a much easier and much smarter way. You can continue using "get" method on your client side and change your server side code from this:

res.jsonp(articles);

where articles is the result array, to this:

res.jsonp(articles[0]);
share|improve this answer
2  
"Use "query" method instead. It works like "get" method totally, but its isArray is set to true naturally." ... spot on for me :-) – Kim Miller Jan 11 '15 at 16:33
    
JSON encoding was my issue. Thanks – Aaron Lelevier Mar 2 '15 at 15:39
    
Saved my life. Thank you! – geoyws Oct 15 '15 at 16:17

You are getting an object that contain two arrays.

My guess is that in your code before you started with the console.log outputs, you were setting categories = response;.

You need to set it to: categories = response.categories;

Here:

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

This might help to understand the problem, also:

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

share|improve this answer

A very simple solve for me from the documentation:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.