3

i'm doing a small application and i realized yesterday about this thing i could not really understand.

I have my controller in Java listening for requests, it returns a Map with more than one value. But when I use ngResource in the controller, i'm calling .get() method instead of .query() one, and then on the view i can iterate over the response i get with the .get() with something like x in response... . Wasn't the query() suposed to be for arrays and get() just for one object?

Could someone explain this please? thanks

EDIT So, the json I'm reciving from {"1":"PRUEBA","2":"HOLA"} is this, and if I use .query() I'm getting the following error:

Error in resource configuration for action Error in resource configuration for action{0}. Expected response to contain an {1} but got an {2} (Request: {3} {4}). Expected response to contain an query but got an array (Request: object GET)

So, I still don't understand why do I need a .get if I'm getting an array...

txomin
  • 177
  • 2
  • 3
  • 15

1 Answers1

1

by default query returns an array and get doesn't

  { 'get':  {method:'GET'},
  'save':   {method:'POST'},
  'query':  {method:'GET', isArray:true},
  'remove': {method:'DELETE'},
  'delete': {method:'DELETE'} };

you can overwrite get or create an other function, with a different url and set isArray to true:

  angular.module('myModule').factory('myResourceFactory', function($resource, apiHost) {
  return $resource(apiHost + '/myresources/:myresourceId', null, {
        'query': { method:'GET', url: apiHost + '/myresources/:myresourceId/somethings',isArray: true}
    }); 
});

The query method expects an array [{obj1},{obj2}]

The get method expects an object {"1":"PRUEBA","2":"HOLA"}

in your case you should use the get() method.

MayK
  • 1,269
  • 1
  • 10
  • 24
  • Ya ok, I understand that, but my controller in Java returns two values on the map, and I call the service by apiHos.myFunction().get(). If I use apiHos.myFunction().query I'm getting an error. I thought that if I would like to return a map object I should do the .query() one, but no, that's what I don't really understand... maybe I'm messing up?? haha – txomin May 18 '16 at 10:18
  • what is the error that you are getting when you use query ? can you give me an example of the result returned by your api ? – MayK May 18 '16 at 10:20
  • sure... but I'm not in front of the code right now... i'll edit the question as soon as possible, thanks @Mayk!!! – txomin May 18 '16 at 10:22
  • I just edited, if more info is needed just tell me, thanks – txomin May 18 '16 at 23:01