mongoosejs / mongoose-lean-getters

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

Error when creating a non-discriminated document on a discriminated model #39

Closed DesignByOnyx closed 3 months ago

DesignByOnyx commented 4 months ago

Do you want to request a feature or report a bug?

Bug.

What is the current behavior?

When using discriminated models, we should still be able to retrieve non-discriminated documents.

If the current behavior is a bug, please provide the steps to reproduce.

const baseSchema = new mongoose.Schema({ foo: String });
const customSchema = new mongoose.Schema({ bar: Number });

const BaseModel = mongoose.model('BaseModel', baseSchema);
const CustomModel = BaseModel.discriminator('Custom', customSchema);

const entry = await BaseModel.create({ foo: "foo" });
await BaseModel.findById(entry._id).lean({ getters: true });

// TypeError: Cannot read properties of undefined (reading 'eachPath')
//    at model.Query.applyGettersToDoc (..../node_modules/mongoose-lean-getters/index.js:110:16)
//    ....

What is the expected behavior?

Should still be able to create non-discriminated documents

What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.

Node: v20.12.2 mongoose-lean-getters: v2.1.0 mongoose: v8.5.1 (also happens with v7.7.0)

NOTE: I have a fix coming