javascript - Mongoose deep populate returning only ID not the entire Object/Array -
this question has been asked death on here, doesn't seem clear cut answer out there.
so have db user has many topics, posts & comments, topic has many posts, , post has many comments.
the adding users, topics, posts & comments working properly. topics being displayed populating posts inside it.(with minor redirection glitch). unable populate comments inside posts. have far.
gettopic method inside topic controller
gettopic: function(req, res){ console.log("get_topic query", req.body._id) topic.findone({_id: req.body._id}).populate('posts').populate('posts.comments').exec(function (err, topic) { if(err){ console.log("something went wrong"); res.json(err) }else{ console.log(topic); res.json(topic); } }) }
topic model
var topicschema = new mongoose.schema({ topic: string, description: string, category: string, post_count: number, user_name: string, _user: {type: schema.types.objectid, ref: 'user'}, posts: [{type: schema.types.objectid, ref: 'post'}], created_at: date });
post model
var postschema = new mongoose.schema({ post: string, up_vote: number, down_vote: number, user_name: string, _user: {type: schema.types.objectid, ref: 'user'}, _topic: {type: schema.types.objectid, ref: 'topic'}, comments: [{type: schema.types.objectid, ref: 'comment'}], created_at: date });
comment model
var commentschema = new mongoose.schema({ comment: string, user_name: string, _user: {type: schema.types.objectid, ref: 'user'}, _post: {type: schema.types.objectid, ref: 'post'}, created_at: date });
find topic.findone working populating "posts", when populating comments, ids show, have console.log in both end , frontend.
as can see hasnt populated arrays, can see [object] in terminal , id's in js console.
what doing wrong?
console.log(json.stringify(topic.posts,undefined,2))
{ "_id": "56b3f865fe0aca747e9dae4f", "topic": "new topic in new db", "description": "testing phase 1", "category": "html", "user_name": "ani", "post_count": 0, "__v": 7, "posts": [ { "_id": "56b3f880fe0aca747e9dae50", "post": "posting answer phase 1", "user_name": "ani", "up_vote": 0, "down_vote": 0, "created_at": "2016-02-05t01:18:56.709z", "__v": 3, "comments": [ "56b40368004a0707806e4b93", "56b4046beef00e4c82126269", "56b406656171e44383374cf0" ] }, { "_id": "56b4061a51c0d3f282b89a95", "post": "answer/post v.2.0", "user_name": "ani", "up_vote": 0, "down_vote": 0, "created_at": "2016-02-05t02:16:58.575z", "__v": 1, "comments": [ "56b4062551c0d3f282b89a96" ] }
console.log(json.stringify(topic,undefined,2))
"posts": [ { "_id": "56b3f880fe0aca747e9dae50", "post": "posting answer phase 1", "user_name": "ani", "up_vote": 0, "down_vote": 0, "created_at": "2016-02-05t01:18:56.709z", "__v": 3, "comments": [ "56b40368004a0707806e4b93", "56b4046beef00e4c82126269", "56b406656171e44383374cf0" ] }, { "_id": "56b4061a51c0d3f282b89a95", "post": "answer/post v.2.0", "user_name": "ani", "up_vote": 0, "down_vote": 0, "created_at": "2016-02-05t02:16:58.575z", "__v": 1, "comments": [ "56b4062551c0d3f282b89a96" ] }
you can try using mongoose-deep-populate plugin nested objects
var deeppopulate = require('mongoose-deep-populate'); topic.findone({ _id: req.body._id }).deeppopulate('posts posts.comments').exec(function (err, topic) { if (err) res.json(err) else{ console.log(topic); res.json(topic); }; });
Comments
Post a Comment