mongoosejs / mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries
Apache License 2.0
45 stars 24 forks source link

RangeError: Maximum call stack size exceeded , when query with lean({virtuals:true}) #33

Closed Mattstacey321 closed 4 years ago

Mattstacey321 commented 4 years ago

This is my console log. image

The following code is not working

const mongoose = require('mongoose');
const mongooseLeanVirtuals = require('mongoose-lean-virtuals');
const { paginate } = require('mongoose-paginate-v2');

//const opts = { toJSON: { virtuals: true } };

const post = mongoose.Schema({
    user_id: String,
    title: String,
    content: String,
    media: {
        default: "",
        type: String
    },
    tag: [
        String
    ],
    created_time: {
        default: Date.now,
        type: Date
    },
    reactions: [
        {
            user_id: String,
            reaction_type: String
        }
    ],

    permission: {
        type: String,
        default: "open"
    }

})
const commentSchema = mongoose.Schema({
    user_id: String,
    content: String,
    media: {
        type: String,
        default:""
    },
    reactions: [
        {
            user_id: String,
            reaction_type: String
        }
    ],
    created_time: {
        default: Date.now,
        type: Date
    },
})
commentSchema.add({
    comments: [
        {
            commentSchema
        }
    ],
})
post.add({
    comments: [
        commentSchema
    ]
})

commentSchema.virtual('comment_id').get(function () {
    return this._id;
});
commentSchema.plugin(mongooseLeanVirtuals);

post.virtual('post_id').get(function () {
    return this._id;
});
post.plugin(mongooseLeanVirtuals);

module.exports = mongoose.model('post', post);