Igor Simic
6 years ago

AngularJS - Expected response to contain an array but got an object


When making API requests with AngularJS sometimes you will get the error saying 
Expected response to contain an array but got an object
and if you take a look in AngularJS docs you will see error message like this:


Error: $resource:badcfgResponse does not match configured parameter

Error in resource configuration for action `query`. Expected response to contain an array but got an object 
(Request: GET your/url/request)
The problem here is that Angular is expecting Array in return but it is getting object.
To fix it, we have to use 'query'  param in $resource request and set isArray to false:
var getContent = $resource('/your/url/request',null,
	{
	    'query':  {method:'GET', isArray:false} // false for object, true for array
	});

// show response
return getContent.query().$promise.then(function(response) {
	// after we have reponse
});