Error: [$ resource: badcfg] Error in resource configuration for action `query`. Expected response contains an array, but received an object
I am very new to MEAN stack development and just started working yesterday. I am trying to get my data back from the database with a call using a resource that is associated with a server side controller. But I am getting the following console error "Error: [$ resource: badcfg] Error in resource configuration for action query
. Expected response contains an array, but received an object"
Angular Controller:
app.
controller('ArticleCtrl', function($scope, $location, $resource){
var articles = $resource('/api/articles');
articles.query(function(result){
console.log(result);
});
$scope.addnew = function(){
$location.path("/administrationarea/articles/newarticle");
}
});
Server.js:
articlesController = require('./server/controller/article-controller');
app.get('/api/articles', articlesController.list);
app.post('/api/articles', articlesController.create);
server controller:
var article = require('../models/articleModel');
module.exports.create = function(req, res){
var art = new article(req.body);
art.save( function (err, result){
res.json(result);
});
}
module.exports.list = function(req, res){
article.find({}, function (err, results){
res.json(results);
});
}
can someone tell me why this might be happening and can suggest a solution so that the data is returned as an array instead of an object.
source to share
The error is that
var articles = $resource('api/articles');
articles.query(function(result){
// here result should be an array.
console.log(result);
});
here articles.query expects an array. but not an object.
if your api is returning an object then you should use articles.get ().
articles.get(function(result){
// here result should be an object but not array
console.log(result);
});
$ resource.query () is used to get an array from the REST API and $ resource.get () is used to get an object from the REST API. but in both cases, the request type (/ method) is only "get".
refer to angularJS documentation
source to share