txomin txomin - 6 months ago 18
AngularJS Question

NgResource get call for requesting a map

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...

Answer

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.

Comments