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:
[{"priority":"ONE","count":5,"globalCount":3037}]
Digging into the angular-resource.js, the exception is thrown here:
if (angular.isArray(data) !== (!!action.isArray)) {
throw $resourceMinErr('badcfg', ...);
}
as expected !!action.isArray
returns true, but strangely angular.isArray(data)
returns false. What's going on here?
The response needs to be transformed from json.
Mixin.staticMethod(Type, null, 'apiResponseTransformer', function (json) {
var data = angular.fromJson(json);
if (angular.isArray(data)) {
return data.map(Type.build).filter(Boolean);
}
return Type.build(data);
})
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments