skip to Main Content

router.get('/mypage/myreport-comment', catchAsync( async(req, res) => {
    const { id } = req.user;
    const { page } = req.query;

    const totalPost = await Comment.find({'reports.user': id}).populate('reports.user'); //.countDocuments({});

    let { startPage, endPage, hidePost, maxPost, totalPage, currentPage } = myPageCommentPaging(page, totalPost);
    const comments = await Comment.find({reports:{user:id}, isDeleted: false}).sort({ createdAt: -1 }).skip(hidePost).limit(maxPost).populate('author').populate('board');
    
res.render('users/myReportComment', {comments, startPage, endPage, totalPage, currentPage, maxPost})

}));
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');

const UserSchema = new Schema({
    nickname: {
        type: String,
        required: true,
        unique: true
    },
    role: {
        type: String,
        enum: ['user', 'master'],
        default: 'user'
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    isWithdrawn: {
        type: Boolean,
        default: false
    }
});

UserSchema.plugin(passportLocalMongoose, {usernameField: 'email'});

module.exports = mongoose.model('User', UserSchema);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;


const CommentSchema = new Schema({
    body: String,
    createdAt: {
        type: Date,
        default: Date.now
    },
    author: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    board: {
        type: Schema.Types.ObjectId,
        ref: 'Board'
    },
    likes: [
        {
            type: Schema.Types.ObjectId,
            ref: "User"
        }
    ],
    parentComment: {
        type: Schema.Types.ObjectId,
        ref: "Comment"
    },
    hasReply: {
        type: Boolean,
        default: false
    },
    isDeleted: {
        type: Boolean,
        default: false
    },
    reports: [
        {
            user: {
                type: Schema.Types.ObjectId,
                ref: "User"
            },
            reportedAt: {
                type: Date,
                default: Date.now
            }
        }
    ]
});

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

I’m trying to find all Comment objects where a specific user is present in the reports field.

I want to populate the ‘user’ field in the ‘reports’ field, but the method .populate( 'reports.user' ) is not working. What should I do?

I tried to output the code const totalPost = await Comment.find({'reports.user': id}).populate('reports.user'); in various ways, but totalPost either outputs an empty array or is displayed in the format reports: [[object]].

It might sound awkward because English is not my native language.

2

Answers


  1. Chosen as BEST ANSWER

    I successfully received the value. Upon checking the database, I found that it stored data like this:

    reports: [
          {
            _id: ObjectId("6530fc986bfs0ec1c28e48e7"),
            reportedAt: ISODate("2023-12-12T08:46:48.524Z")
          }
    ],
    

    So, I entered the code as follows: const totalPost = await Comment.find({'reports._id': id}) and I was able to retrieve the desired value. It's not completely resolved yet, but thanks to everyone for the help.


  2. i think you should use

    const totalPost = await Comment.find({
          'reports': {
            $elemMatch: {
              'user': id
            }
          }
        });
    

    cause $elemMatch operator is useful for finding in array of object where there is at least one element in array.

    for more info: https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/

    then you can add populate method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search