Skip to content

Commit d27185f

Browse files
authored
Merge pull request #40 from DesignByOnyx/39-non-discriminated-documents
fix: allow non-discriminated documents be retrieved
2 parents b1b7265 + aa5bf99 commit d27185f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ function getSchemaForDoc(schema, res) {
8181
break;
8282
}
8383
}
84-
return childSchema;
84+
85+
// If no discriminator schema found, return the root schema (#39)
86+
return childSchema || schema;
8587
}
8688

8789
function applyGettersToDoc(schema, doc, fields, prefix) {

test/index.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,18 @@ describe('mongoose-lean-getters', function() {
400400
assert.equal(typeof doc.field, 'string');
401401
assert.strictEqual(doc.field, '1337');
402402
});
403+
404+
it('should allow non-discriminated documents to be retrieved (#39)', async() => {
405+
const baseSchema = new mongoose.Schema({ foo: String });
406+
baseSchema.plugin(mongooseLeanGetters);
407+
408+
const BaseModel = mongoose.model('BaseModel-gh-39', baseSchema);
409+
BaseModel.discriminator('Custom', new mongoose.Schema({}));
410+
411+
// Simply retrieving the non-discriminated document causes the error
412+
await assert.doesNotReject(async() => {
413+
const entry = await BaseModel.create({ foo: 'foo' });
414+
await BaseModel.findById(entry._id).lean({ getters: true });
415+
});
416+
});
403417
});

0 commit comments

Comments
 (0)