简体   繁体   中英

Mongoose deep populate returning only ID not the entire Object/Array

This question has been asked to death over here, but doesn't seem like a clear cut answer is out there.

So I have a DB where a User has many Topics, Posts & Comments, A Topic has many posts, and a Post has many Comments.

The adding Users, Topics, Posts & Comments is working properly. The Topics are also being displayed with populating the posts inside it.(with minor redirection glitch). But I am unable to populate the Comments inside the posts. This is what I have so far.

Gettopic method inside the 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 is working properly even populating the "Posts", but when populating the comments, only the Ids show, have console.log in both back end and frontend.

Chrome控制台

终奌站

As you can see it hasnt fully populated the arrays, can only see it as an [OBJECT] in the terminal and Id's in JS Console.

What am i 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 for deeply 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);
        };
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM