【问题标题】:Mongoose deep populate returning only ID not the entire Object/Array猫鼬深度填充仅返回 ID 而不是整个对象/数组
【发布时间】:2016-05-16 04:23:33
【问题描述】:

这个问题已经在这里被问死了,但似乎没有一个明确的答案。

所以我有一个数据库,其中一个用户有很多主题、帖子和评论,一个主题有很多帖子,一个帖子有很多评论。

添加用户、主题、帖子和评论工作正常。主题也通过填充其中的帖子来显示。(带有轻微的重定向故障)。但我无法在帖子中填充评论。这就是我到目前为止所拥有的。

主题控制器中的Gettopic方法

  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);
        }
        })
    }

主题模型

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
});

后模型

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
});

评论模型

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 甚至在填充“帖子”时也能正常工作,但是在填充 cmets 时,只有 ID 显示,在后端和前端都有 console.log。

如您所见,它尚未完全填充数组,只能在终端中将其视为 [OBJECT] 并在 JS 控制台中将其视为 Id。

我做错了什么?

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"
      ]
    }

【问题讨论】:

  • 请显示对象“文本”而不​​是屏幕截图。这实际上就是为什么图像在所需的代表上限下被新海报阻止的原因。在文本更合适时阻止发布图像。
  • 没有对象“文本”,在终端中我得到的只是 [对象] 和浏览器控制台 cmets:数组 [3] 0:“56b40368004a0707806e4b93”1:“56b4046beef00e4c82126269”2: "56b406656171e44383374cf0"
  • 试图 console.log topics.posts .. 它将带有 cmets 的帖子打印为一个数组,但只有以前的 Id。 console.log topic.posts.cmets 回馈 undefined。
  • 基本序列化console.log(JSON.stringify(topic,undefined,2))。由于默认情况下,“对象”的字符串化不会在日志记录中发生那么深。我所说的“文本”是指从控制台“粘贴”“文本”。不是截图。
  • 更新了主帖。不会再上传图片了。对此感到抱歉。

标签: javascript node.js mongodb mongoose mean-stack


【解决方案1】:

您可以尝试使用mongoose-deep-populate 插件处理深度嵌套的对象

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);
        };
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-24
    • 2016-04-28
    • 2019-07-17
    • 2015-02-09
    • 2014-11-25
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多