0

I ve got an angular resource service which then returns the data to a controller and I get all the data plus the data by name.

My application works just fine in the browser but I get a resource error in the console. Bad resource configuration.

I had a look in various questions and everyone states that I need to set the configuration property isArray to either false or true.

I have tried to do this but I still get an error.

Any ideas much appreciated.

Here is my service :

(function() {

  var app = angular.module('test');

  app.service('ContactResource', function($resource) {

      return $resource('/contacts/:firstname', {},
        {'update': {method: 'PUT'}},
        {'query': { method: 'GET', isArray: true }},
        {'get': { method: 'GET', isArray: false }}
      );
  });

}());

And here is my controller:

(function() {

  var app = angular.module('test');

  app.controller('contactsCtrl', function($scope, $routeParams, ContactResource) {
    $scope.contacts = ContactResource.query();

    $scope.singlecontact = ContactResource.get({firstname: $routeParams.firstname});
  });

}());

The error I am getting is : Error: [$resource:badcfg] http://errors.angularjs.org/1.4.2/$resource/badcfg?p0=get&p1=object&p2=array&p3=GET&p4=%2Fcontacts

When I click it says :

Error in resource configuration for action get. Expected response to contain an object but got an array (Request: GET /contacts)

When I get the url is /contacts the response is :

[{EmailAddress:[email protected], etc}]

When the url is /contacts/firstname the response is :

{EmailAddress:[email protected],etc}

17
  • Here's a trick that should serve you for your whole career as a programmer: when you get an error, read it and try to make sense of it. If you don't understand it and ask a question, post it, because other people probably can understand it, or at least have already seen it happen. Ignoring the error is the best way to look for problems at the wrong places. Don't paraphrase the error, don't post two words of it, don't post just the message. Post the whole stack trace. Commented Sep 12, 2015 at 15:39
  • can you post the full error that you are receiving? Commented Sep 12, 2015 at 15:41
  • I have now updated the post with the error. Thanks for the advice guys Commented Sep 12, 2015 at 15:42
  • 1
    well, that error says the server returned an array, but you told angular you would be getting a single object. What is returned if you just put the http://example.com/contacts/somefirstname in the address bar? a single item or an array? Commented Sep 12, 2015 at 15:43
  • It is a single object Commented Sep 12, 2015 at 15:45

1 Answer 1

0

I solved the problem by adding a new controller called single controller and by separating the service into two functions. Here is how my code looks like now.

This is the service:

(function() {

  var app = angular.module('test');

  app.service('ContactResource', function($resource, $routeParams) {

    this.all = function() {
      return $resource('/contacts', {},
      {'query': { method: 'GET', isArray: true }}
    )};

    this.single = function() {
      return $resource('/contacts/:firstname', {firstname: '@firstname'},
        {'query': { method: 'GET', isArray: false }}
      );
    }
  });

}());

And the controllers :

(function() {

  var app = angular.module('test');

  app.controller('contactsCtrl', function($scope, $routeParams, ContactResource) {

    $scope.contacts = ContactResource.all().query();

  });

  app.controller('singleCtrl', function($scope, $routeParams, ContactResource) {

    $scope.singlecontact = ContactResource.single().query({firstname: $routeParams.firstname});

  });

}());

For some reason which I am still not sure $resource wouldn't accept them into the same controller.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.