These are chat archives for Automattic/mongoose

4th
Aug 2017
Xiaotian Gu
@Xiaoy312
Aug 04 2017 20:00
Hi, can anyone tell me why the 2nd piece of code doesnt work?
Model.find(...)
  .populate('child')
  .populate('child.grandchild') // this works
Model.find(...)
  .populate('children')
  .populate('children.grandchild') // this doesnt work
Chuan Qin
@pantherqin
Aug 04 2017 20:06
Need to check your schema for the model.
Xiaotian Gu
@Xiaoy312
Aug 04 2017 20:11
here is my schemas:
var discussionSchema = new Schema({
    _id: Schema.Types.ObjectId,
    author: {ref: 'User', type: Schema.Types.ObjectId},
    comments: [{ref: 'Comment', type: Schema.Types.ObjectId}],
    //...
});
var commentSchema = new Schema({
    _id: Schema.Types.ObjectId,
    author: {ref: 'User', type: Schema.Types.ObjectId},
    //...
});
var userSchema = new Schema({
    _id: Schema.Types.ObjectId,
    //...
});
And the actual query Im trying to make:
Discussion
    .findById(req.params.discussionId, 'title content author subject likes dislikes comments')
    .populate('author', 'username first_name last_name profile_picture')
    .populate('author.profile_picture', 'view url')
    .populate('comments')
    .populate('comments.author', 'username first_name last_name profile_picture')
    //.populate('comments.author.profile_picture', 'view url')
Chuan Qin
@pantherqin
Aug 04 2017 20:18
so you are saying populate author and populate author.profile_picture works, but comments and comments.author doesn't?
Xiaotian Gu
@Xiaoy312
Aug 04 2017 20:24
everything except 'comments.author' worked
Chuan Qin
@pantherqin
Aug 04 2017 20:29
well... then that s really weird..
You may want to try Populating across multiple levels in http://mongoosejs.com/docs/populate.html
Or revisit your comments schema to see if there is any typo... Or remove the first 2 populate to see if the next 2 works alone...
Xiaotian Gu
@Xiaoy312
Aug 04 2017 20:35
removing them doesnt help, and there is no typo in what i pasted here
but, ill look into the object syntax