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?
Add {}
after '/posts'
.
bulletinApp.factory('Post', ['$resource', function($resource) {
return $resource('/posts', {}, {'query': {method: 'GET', isArray: false}});
}]);
For details, see this post.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With