It appears that Mongoose automatically populates an array when using findById().populate(), however, the same does not apply when using find().populate(). I have found that I need to use findById()...
Why do you think find().populate() is not functioning as expected?
Here is a snippet of the code (express route):
//Retrieve a user's details, including their friends, for displaying.
app.get("/friends", function(req, res){
//People.find({name: 'Tom'}).populate("friends").exec(function(err, foundUser){
People.findById("5c37f2d67c8").populate("friends").exec(function(err, foundUser){
console.log("! My friends: " + foundUser.friends); //Is Undefined if using find()... but NOT if using findById(). Weird.
if(err){
console.log(err);
res.redirect("/");
} else {
//res.send(foundUser); //When I pass foundUser to the View, foundUser.friends is ALWAYS populated / never undefined, regardless of using find() or findUser(). Weird, as the above console.log() is undefined if using find().
res.render("peopleIndex", {foundUser: foundUser});
}
});
});
Edit: SOLUTION: If anyone's wondering, you can use findOne() instead of find() to ensure it populates correctly before passing to the view. (Still unsure why find() doesn't work.)