猫鼬模型未填充参考模型

时间:2020-06-19 12:58:52

标签: node.js mongodb mongoose nosql

我正在尝试填充评论数组 但它没有人口 我有

我的帖子模型

const mongoose = require('mongoose');

const postSchema = new mongoose.Schema(
  {
    caption: String,
    tags: {},
    hashtags: {},
    location: {},
    images: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'GFS',
      },
    ],
    comments: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Comment',
      },
    ],
    author: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    },
  },
  {
    timestamps: true,
  }
);



module.exports = mongoose.model('Post', postSchema, 'posts');

评论模型

const mongoose = require('mongoose');

const commentSchema = new mongoose.Schema(
  {
    text: {
      type: String,
      required: true,
    },
    author: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    },
    postId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Post',
    },
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model('Comment', commentSchema, 'comments');

这是我获取带有评论的帖子的功能

exports.getPosts = async (req, res) => {
  try {
    const posts = await Post.find()
      .select('caption location')
      .populate('comments')
      .exec();


    res.status(200).json({ posts });

  } catch (error) {
    console.log(error);
  }
};

,但根本没有填充 我以前使用过虚拟裁判,而民居对我来说是虚拟的,但这一次我想使用裁判 但没有填充评论

0 个答案:

没有答案