Eunice Chia Eunice Chia - 1 year ago 68
Node.js Question

mongoose callback is not an array

In my models I have this

module.exports.getPhotosById = function(userId,callback){

Then in route I do



The first console output this

{ _id: 325657865435643245,
userId: '32443564',
photo: 'abc.jpg',
caption: 'abc'

but why it's not an array? because the second console's output is

Answer Source

result is a single document instead of an array because you're calling findOne, not find.

To get all of a user's photo docs, change your method to:

module.exports.getPhotosById = function(userId, callback){
    Photos.find({userId: userId}, callback);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download