Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get an array of numbers from angular.js resource?

my RESTful API returns an array:

GET /test => [1367297123312,1.0,2.0,3.0,100]

I have a service:

(angular
 .module('app.services', ['ng', 'ngResource'])
 .factory('myData', [
     /******/ '$resource',
     function ($resource) {
         return $resource('test');
     }])
);

In my controller I need to get the numbers. I tried:

(angular
 .module('app.controllers', ['ng', 'app.services'])
 .controller('tweetsapiContr', [
     /******/ '$scope', 'myData',
     function ($scope,   myData) {
         myData.get({}, function (data) {
             console.log(data);
         };
     }
 ])
);

The above gives me TypeError: Object #<h> has no method 'push' error, and if I use query instead of get on the service, it returns an array of objects that have methods like $get, $save etc, but calling $get for example returns undefined.

How to get the numbers? Responding with a hash from the server works, but I am trying to figure out how to make it work with arrays.

like image 603
akonsu Avatar asked Apr 30 '13 05:04

akonsu


3 Answers

A resource "class" object with methods for the default set of resource actions optionally > extended with custom actions. The default set contains these actions:

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

So you have two options:

Set a custom method on the resource object:

$resource("/url/:someParam", {}, {
    getMyArray: {method:"GET", params: {someParam:"hello"}, isArray: true}
});

Use query method

Resource object has a query method which is defined with isArray: true as you can see in the top of the answer.

Please be advised that responding with a top level array to a GET method has security vulnerabilities as array constructors can be redefined.

Read those: JSON security best practices? What are "top level JSON arrays" and why are they a security risk?

like image 108
Umur Kontacı Avatar answered Oct 12 '22 12:10

Umur Kontacı


This open issue (#4314) has good information about why numbers (and other primitive data types like strings) cannot be parsed by $resource.

From what I understand, $resource needs to work on objects so that it can attach it's methods (get, query, post, etc...). If all you are trying to do is to read the array and never need to 'update' it, they recommend using just straight up $http.

Hope that helps clear things up and points future readers of this question in the right direction for best practices in dealing with arrays over a REST api.

like image 42
jlee Avatar answered Oct 12 '22 13:10

jlee


You can add a custom action to the resource with isArray: true; (resource docs)

return $resource('test', {}, {
  getArray: { method: 'GET', isArray: true } 
};

Then I believe you have to call it with a dollar sign myData.$getArray

like image 45
Bogdan Rybak Avatar answered Oct 12 '22 13:10

Bogdan Rybak