mon goose модель не заполняется refrence model - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь заполнить массив комментариев, но он не заполняется. У меня есть

Моя модель сообщения

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

, но он вообще не заполняется, я использовал виртуальные ссылки раньше, и население работает в виртуальном для меня, но на этот раз я хотел использовать ref, но не заполнял комментарии

...