cscan cscan - 1 year ago 148
AngularJS Question

Angular.isArray(data) returns false for an Array

I'm using angular's $resource to get data from an API. My angular $resource is configured as follows:

Priorities : $resource (baseUrl + 'priorities/:priorityType/:uuid/all', {}, {
query : {
method: 'GET',
params: {
priorityType : '@priorityType',
uuid : '@uuid'
isArray: true

However, when calling Priorities.query a $resource:badcfg error is thrown: "Expected response to contain an array but got an object". This exception means that the API returned an object but $resource is configured to receive an array - but the API is clearly returning an array:


Digging into the angular-resource.js, the exception is thrown here:

if (angular.isArray(data) !== (!!action.isArray)) {
throw $resourceMinErr('badcfg', ...);

as expected
returns true, but strangely
returns false. What's going on here?

Answer Source

The response needs to be transformed from json.

Mixin.staticMethod(Type, null, 'apiResponseTransformer', function (json) {
    var data = angular.fromJson(json);
    if (angular.isArray(data)) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download