2

I have an angular resource factory, which returns a JSON object and looks like this:

angular.module('myApp').factory('resourceProvider', function ($resource, $http) {
return {
Attribute: $resource('http://localhost:49980/api/Attribute/:id', 
{ id: '@id' }, { query: { method: 'GET', isArray: false } }),     
};
});

When I 'query' the resource like this:

resourceProvider.Attribute.query(function (data) {
  $scope.variable = data;
});

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

This seems really strange to me because I'm setting isArray to false. Furthermore the code is working fine on other pcs. So I would be really thankful if anyone had an idea where the error could come from.

2
  • You can't just assign a method to an Object property, you need to create a function and return it. Commented Jan 17, 2014 at 13:46
  • Sorry I think you missunderstood me. I added the code where I query the resource Commented Jan 17, 2014 at 14:07

2 Answers 2

2

Problem solved. There was a problem with the db authenication and the error message happend to be a JSON object. So there was actually a JSON object returned instead of an array.

Sign up to request clarification or add additional context in comments.

1 Comment

I have this exact scenario too, however the JSON object is the expected response on server errors. It'd be nice if $resource could be configured to handle this without sending errors through Angular's $log service. It especially adds noise to our tests, where we intentionally confirm how the error handling is supposed to work.
1

Question1: Why are you using angulars $resource-service and not $http-service?
Question2: If you want to use $resource, why you whant to override $resource's default behavior?
See Angular Docs

And im not sure but, do you have a typo in

return {
Attribute: $resource('http://localhost:49980/api/Attribute/:id', 
{ id: '@id' }, { query: { method: 'GET', isArray: false } }),     
};

1 Comment

Thanks for your answer. But you must have overread that there is actually nothing wrong with the code. The project works fine on other pcs. The error occurs only on my computer (after I checked it out from subversion). So my question was if anyone has a clue where the problem could come from.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.