Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

Tags:

angularjs

I am a complete noob with respect to angular, just going through a codeschool tutorial and I've hit my first hurdle.

I am getting Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an array but got an object

Here is my code:

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

bulletinApp.controller('PostsController', ['$scope', 'Post', function($scope, Post) {
    $scope.heading = 'Welcome';
    $scope.posts = Post.query();

    $scope.newPost = {
        title: 'Test Post'
    }

}]);

bulletinApp.factory('Post', ['$resource', function($resource) {
    return $resource('/posts');
}]);

I found an answer here: Error in resource configuration while using $resource.query() function with AngularJS/Rails

That says I should add this to my resource declaration:

'query': {method: 'GET', isArray: false }

Problem being I have no idea where I am supposed to add this, or even if this is the problem I'm having?

EDIT:

bulletinApp.factory('Post', ['$resource', function($resource) {
  return $resource('/posts', {'query': {method: 'GET', isArray: false}});
}]);

After this I am still receiving the error. Also in the tutorial, there was no need for this and I've followed it pretty much to a tee, why would the get method be getting an object instead of an array?

like image 995
Melbourne2991 Avatar asked Dec 02 '13 05:12

Melbourne2991


2 Answers

Add {} after '/posts'.

bulletinApp.factory('Post', ['$resource', function($resource) { 
    return $resource('/posts', {}, {'query': {method: 'GET', isArray: false}}); 
}]);

For details, see this post.

like image 87
jpark7ca Avatar answered Oct 22 '22 05:10

jpark7ca


The result of 'Get' is array or just object? If it's an array, set isArray=true, else set isArray=false, like the guy said above

bulletinApp.factory('Post', ['$resource', function($resource) { 
return $resource('/posts', {}, {'query': {method: 'GET', isArray: false/*true*/}}); 
}]);

And attention IsArray is different with isArray. Or if you got your data by json, please add *.json as url

like image 6
CLoren Avatar answered Oct 22 '22 04:10

CLoren