Can't get Mongoose.js Subdocument Array to populate

前端 未结 5 431
孤独总比滥情好
孤独总比滥情好 2021-02-04 06:23

I\'m using mongoose.js on a node.js server connecting to mongodb and I have a mongoose model like the following

SubSchema = new Schema({
    _member:     {type:          


        
5条回答
  •  耶瑟儿~
    2021-02-04 06:57

    @leesei: I can't comment on your post (too little rep), so I leave this as a separate answer.

    In mongoose 3.6 subdoc population still doesn't work, the issue github.com/LearnBoost/mongoose/issues/1381 has been closed 7 months ago with the following solution as a workaround. I had to change it slightly to merge the populated subdocument back to the main document.

    The subdocument's model Story has to be specified explicitly:

    Person.findById(user1._id).populate("stories")
        .exec(function(err, doc {
             Story.populate(doc.stories, {path: 'creator'}, function (err, stories) {
                 doc.stories = stories;
                 return doc;
             })
    })
    

    In the solution above this works:

    Story.populate(doc.stories, {path: 'creator'}, callback)
    

    but this still won't work:

    Story.populate(doc, {path: 'stories.creator'}, callback)
    

提交回复
热议问题