Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // z Post.js
- postSchema.virtual("comments", {
- ref: "Comment",
- localField: "_id",
- foreignField: "author"
- });
- //Comment.js
- const mongoose = require("mongoose");
- const commentSchema = mongoose.Schema(
- {
- commentedPostId: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "Post",
- required: true
- },
- image: {
- type: Buffer
- },
- author: {
- type: mongoose.Schema.Types.ObjectId,
- ref: "User",
- required: true
- },
- body: {
- type: String
- }
- },
- {
- timestamps: true
- }
- );
- const Comment = mongoose.model("Comment", commentSchema);
- module.exports = Comment;
- // z kontrolera post.js
- const { id } = req.params;
- const post = await Post.findById(id);
- await post.populate("comments").execPopulate();
- res.status(200).send({
- post,
- comments: post.comments
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement