mongoosejs / mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
Apache License 2.0
11 stars 16 forks source link

Chaining getter on virtual doesn't work with lean #10

Closed MarcusElevait closed 2 years ago

MarcusElevait commented 2 years ago

We have the following virtual defined in our schema:

AnnotationTypeSchema.virtual('instances', {
    ref: AnnotationType.name,
    localField: 'classToAnnotate',
    foreignField: 'classToAnnotate',
    match: { textField: { $exists: true } },
}).get((instances: BaseAnnotationType[]) => instances.map(instance => instance.textField));

And we're trying to execute the following query:

const items = await this.annotationTypeModel
            .find(searchFilter)
            .limit(filter.limit)
            .skip(filter.limit * (filter.page - 1))
            .sort({ title: 1 })
            .populate('instances')
            .lean({ virtuals: true, getters: true })
            .exec();

But we get the error, that the instances is undefined and therefore the map function cannot be executed.

Don't know if the intended behavior of the Plugin should cover such cases.

vkarpov15 commented 2 years ago

You should open this issue on mongoose-lean-virtuals. This project only runs getters on schema paths.