angularjs - Error: [$resource:badcfg] Error in resource configuration for action `query`. Expected response to contain an array but got an object -
i new mean stack development , started yesterday. trying data database call using resource linked server side controller. receiving following console error "error: [$resource:badcfg] error in resource configuration action query
. expected response contain array got 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 side 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 tell me why might happening , maybe offer solution data returned array instead of object.
the error because
var articles = $resource('api/articles'); articles.query(function(result){ // here result should array. console.log(result); });
here articles.query expects array. not object.
if api returns object should use articles.get().
articles.get(function(result){ // here result should object not array console.log(result); });
$resource.query() used getting array rest api , $resource.get() used getting object rest api. in both cases request type(/method) 'get' only.
please refer angularjs documentation
Comments
Post a Comment