-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Description
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
We have a schema with (1) nested documents, which (2) use discriminators, and where (3) (only) some of the discriminated types uses a refPath. With 5.9.9 and 5.9.10 we get an error Cannot read property 'schema' of undefined when we try to populate this reference.
If the current behavior is a bug, please provide the steps to reproduce.
import mongoose from 'mongoose';
beforeAll(async () => mongoose.connect(process.env.MONGO_URL as string));
afterAll(async () => mongoose.disconnect());
it('Mongoose getModelsMapForPopulate issue', async () => {
const nested = new mongoose.Schema(
{
// nothing here
},
{
discriminatorKey: 'type'
}
);
const main = new mongoose.Schema({
items: [nested]
});
const nestedDiscriminated = new mongoose.Schema({
refPath: { type: String, required: true },
ref: { type: mongoose.Types.ObjectId, refPath: 'items.refPath' }
// just for illustration; this works:
// ref: { type: mongoose.Types.ObjectId, ref: 'getModelsMapForPopulateIssue_other' }
});
const itemsType = main.path('items') as mongoose.Schema.Types.DocumentArray;
itemsType.discriminator('discriminated', nestedDiscriminated);
const MainModel = mongoose.model<any>('getModelsMapForPopulateIssue_main', main);
const OtherModel = mongoose.model<any>(
'getModelsMapForPopulateIssue_other',
new mongoose.Schema({
name: { type: String }
})
);
const otherDoc = new OtherModel({ name: 'hello world' });
await otherDoc.save();
await new MainModel({
items: [
{
type: 'not_discriminated'
},
{
type: 'discriminated',
refPath: 'getModelsMapForPopulateIssue_other',
ref: otherDoc._id
}
]
}).save();
const result = await MainModel.find({}).populate('items.ref').exec();
expect(result[0].items[1].ref.name).toEqual('hello world');
});This might be a regression related to this?
What is the expected behavior?
The test should pass, and the document should be populated. Instead we’re now receiving the following error:
TypeError: Cannot read property 'schema' of undefined
at getModelsMapForPopulate (node_modules/mongoose/lib/helpers/populate/getModelsMapForPopulate.js:232:94)
at populate (node_modules/mongoose/lib/model.js:4313:21)
at _populate (node_modules/mongoose/lib/model.js:4283:5)
at node_modules/mongoose/lib/model.js:4258:5
at promiseOrCallback (node_modules/mongoose/lib/helpers/promiseOrCallback.js:9:12)
at Function.Model.populate (node_modules/mongoose/lib/model.js:4256:10)
at cb (node_modules/mongoose/lib/query.js:1934:17)
at node_modules/mongodb/lib/utils.js:731:5
at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
at node_modules/mongodb/lib/cursor.js:841:66
at handleCallback (node_modules/mongodb/lib/utils.js:128:55)
at completeClose (node_modules/mongodb/lib/cursor.js:929:16)
at Cursor.close (node_modules/mongodb/lib/cursor.js:948:12)
at node_modules/mongodb/lib/cursor.js:841:27
at handleCallback (node_modules/mongodb/lib/core/cursor.js:32:5)
at node_modules/mongodb/lib/core/cursor.js:685:38
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
- Node.js v13.8.0
- Mongoose 5.9.10
- MongoDB 3.6.7
danielesser
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.