-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot read property 'schema' of undefined for discriminated nested array when using refPath
#8837
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
AbdelrahmanHafez
added
the
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
label
Apr 22, 2020
Playing around with this, I can confirm that this error appears, slightly modified the reproduction script: 'use strict';
const mongoose = require('./');
const { Schema } = mongoose;
const assert = require('assert');
run().catch(console.error);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const nestedSchema = new Schema({}, { discriminatorKey: 'type' });
const mainSchema = new Schema({ items: [nestedSchema] });
const nestedDiscriminated = new Schema({
modelName: { type: String, required: true },
otherId: { type: mongoose.Types.ObjectId, refPath: 'items.modelName' }
});
const itemsType = mainSchema.path('items');
itemsType.discriminator('discriminated', nestedDiscriminated);
const Main = mongoose.model('Main', mainSchema);
const Other = mongoose.model('Other', new Schema({ message: String }));
const other = await Other.create({ message: 'hello world' });
await Main.create({
items: [
{ },
{
type: 'discriminated',
modelName: 'Other',
otherId: other._id
}
]
});
const result = await Main.findOne().populate('items.otherId');
assert.equal(result.items[1].otherId.message, 'hello world');
console.log('Done.');
} |
AbdelrahmanHafez
added
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
and removed
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
labels
Apr 26, 2020
A bit nicer script as I work to understand the issue more: 'use strict';
const mongoose = require('./');
const { Schema } = mongoose;
const assert = require('assert');
run().catch(console.error);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const eventSchema = new Schema({}, { discriminatorKey: 'type' });
const eventsListSchema = new Schema({ events: [eventSchema] });
const clickEventSchema = new Schema({
modelName: { type: String, required: true },
productId: { type: Schema.ObjectId, refPath: 'events.modelName' }
});
const eventsSchemaType = eventsListSchema.path('events');
eventsSchemaType.discriminator('ClickEvent', clickEventSchema);
const EventsList = mongoose.model('EventsList', eventsListSchema);
const Product = mongoose.model('Product', new Schema({ name: String }));
const product = await Product.create({ name: 'GTX 1050 Ti' });
await EventsList.create({
events: [
{ },
{
type: 'ClickEvent',
modelName: 'Product',
productId: product._id
}
]
});
const result = await EventsList.findOne().populate('events.productId');
assert.equal(result.events[1].productId.name, 'GTX 1050 Ti');
} |
AbdelrahmanHafez
added a commit
to AbdelrahmanHafez/mongoose
that referenced
this issue
Apr 27, 2020
AbdelrahmanHafez
added a commit
to AbdelrahmanHafez/mongoose
that referenced
this issue
Apr 27, 2020
vkarpov15
added a commit
that referenced
this issue
Apr 30, 2020
check discriminator existence before accessing schema in getModelsMapForPopulate
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 errorCannot 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.
This might be a regression related to this?
#8731
What is the expected behavior?
The test should pass, and the document should be populated. Instead we’re now receiving the following error:
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
The text was updated successfully, but these errors were encountered: