0

In my models I have this

module.exports.getPhotosById = function(userId,callback){
    Photos.findOne({userId:userId},callback);
}

Then in route I do

 Photo.getPhotosById(req.user._id,function(err,result){

    console.log(result);
    console.log(result.length);
  });

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 undefined.

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

findone Returns one document(JSON) that satisfies the specified query criteria

find Returns the array of Objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.