0

I have an API returning the following response

{
   "keyA": [1,2,5],
   "keyB": [3,6,4],
   "keyC": [3,2,1]
}

And I'm using a very simple service to access the API endpoint

var $module = angular.module('mySimpleService', ['ngResource']);

$module.service('MyApiService', function(
    $resource
){
    return {
        entities: $resource('http://myapi.com/data', null, {
            'get': {
                method: 'GET',
                isArray: false,
                responseType: 'json'
            }
        })
    };
});

And I'm calling the service like this

var apiData = MyApiService.entities.get()
.$promise.then(function(data){
    console.log(data);  
    console.log(Object.keys(data)); 
});

When I log out the data and the object keys, I can see that angular has added $promise and $resolved as properties to my data. Since I need to enumerate the keys, this is a bit inconvenient.

Am I missing a crucial step here? How can I access my raw untainted response?

gargantuan
  • 8,888
  • 16
  • 67
  • 108

1 Answers1

1

Rather than using the promise itself you can use the callback to .get to get the entity instance.

EntitiesApiService.entities.get(function (entity) {
    // entity is unpolluted
});

You could still use .$promise to check for failures with null, function (error) { and the like.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405