最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Expected response to contain an array but got an object - Stack Overflow

programmeradmin1浏览0评论

So I'm new in Angular, and I've looked over various other solutions, but no one seemed to work for me. My app needs to take some data from mongodb database and show it to the client. The thing is I get

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

Here is my SchoolCtrl.js on the client

app.controller('SchoolsCtrl', function($scope, SchoolResource) {
    $scope.schools = SchoolResource.query();
});

Here is my ngResource

app.factory('SchoolResource', function($resource) {
    var SchoolResource = $resource('/api/schools/:id', {id: '@id'}, { update: {method: 'PUT', isArray: false}});
    return SchoolResource;
});

This is my SchoolsController on the server

var School = require('mongoose').model('School');

module.exports.getAllSchools = function(req, res, next) {
    School.find({}).exec(function(err, collection) {
        if(err) {
            console.log('Schools could not be loaded: ' + err);
        }

        res.send(collection);
    })
};

I tried adding IsArray: true, tried adding [] after 'SchoolResource' in the resource, tried changing routes, nothing worked. I wanted to see what actually is returned, that the query plained is not array, so I turned it to string and this was the result:

function Resource(value) { shallowClearAndCopy(value || {}, this); }

I have no idea why it returns a function. Can anyone help me?

So I'm new in Angular, and I've looked over various other solutions, but no one seemed to work for me. My app needs to take some data from mongodb database and show it to the client. The thing is I get

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

Here is my SchoolCtrl.js on the client

app.controller('SchoolsCtrl', function($scope, SchoolResource) {
    $scope.schools = SchoolResource.query();
});

Here is my ngResource

app.factory('SchoolResource', function($resource) {
    var SchoolResource = $resource('/api/schools/:id', {id: '@id'}, { update: {method: 'PUT', isArray: false}});
    return SchoolResource;
});

This is my SchoolsController on the server

var School = require('mongoose').model('School');

module.exports.getAllSchools = function(req, res, next) {
    School.find({}).exec(function(err, collection) {
        if(err) {
            console.log('Schools could not be loaded: ' + err);
        }

        res.send(collection);
    })
};

I tried adding IsArray: true, tried adding [] after 'SchoolResource' in the resource, tried changing routes, nothing worked. I wanted to see what actually is returned, that the query plained is not array, so I turned it to string and this was the result:

function Resource(value) { shallowClearAndCopy(value || {}, this); }

I have no idea why it returns a function. Can anyone help me?

Share Improve this question asked Jan 13, 2015 at 20:30 nelfurionnelfurion 1411 silver badge9 bronze badges 2
  • Invoke the function (function()) to get your array. – Code Whisperer Commented Jan 13, 2015 at 20:38
  • Can you tell me which function? I tried the resource, tried the controller(though I don't know why would it work if I invoke the controller before it could get the data), but it didn't work. – nelfurion Commented Jan 13, 2015 at 21:06
Add a ment  | 

2 Answers 2

Reset to default 5

That error message usually means your server is returning JSON representing a single object, such as:

{"some": "object", "with": "properties"}

when Angular is expecting JSON representing an array, like:

[ {"some": "object", "with": "properties"}, {"another": "object", "with": "stuff"} ]

Even if there is only a single result, query expects JSON for an array:

[ {"a": "single", "result": "object"} ]

You can verify this by simply loading your API call into the browser and checking it out. If there aren't square brackets around the whole JSON response, it isn't an array.

It happened to me too, but then I printed the object in the console and found out that there was something like this:

{ options:{....},results:[ ...the Array I was looking for... ]}

so all you need to do there is

res.send(collection.results);

I hope this helps

发布评论

评论列表(0)

  1. 暂无评论