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
Discriminator schemas in browser cannot access nested objects in parent schema #5028
Comments
This isn't really a bug. The schemas are only mutated server side, so |
@vkarpov15 I want to be sure I understand, I canNOT use the mongoose library if I am using discriminators in my model definitions? My understanding here is that it will work but childSchema do not inherit properties from base schema? |
@gilles-yvetot if you're just using schema inheritance it will work fine. But unfortunately the current implementation of the browser library doesn't have models, so |
@vkarpov15 but is schema inheritance not obtained from |
@gilles-yvetot can you please provide an example of how you're defining your discriminators? |
const ClickSchema = new Schema(
{
view: {
clickCount: {
type: Number,
default: 1,
min: 1,
},
},
},
{ discriminatorKey: `type` },
)
const StepSchema = new Schema(
{
view: {
url: {
type: String,
trim: true,
},
},
},
{ timestamps: true, discriminatorKey: `type` },
)
const Step = model(`Step`, StepSchema)
const Click = Step.discriminator(`click`, ClickSchema) |
@gilles-yvetot unfortunately not, because Mongoose's browser library currently doesn't support models. A workaround you could try is to use schema composition, like: const ClickSchema = new Schema([StepSchema, {
view: {
clickCount: {
type: Number,
default: 1,
min: 1,
},
},
}], { discriminatorKey: `type` }); And export schemas rather than models. |
@vkarpov15 thanks for the tip! I did not know we could pass an array to the schema constructor. I have tried with the version 5.9.28 but it was not working (not in a browser environment): const { Schema, model } = require('mongoose')
const StepSchema = new Schema(
{
view: {
url: {
type: String,
trim: true,
},
},
},
{ timestamps: true, discriminatorKey: `type` },
)
const ClickSchema = new Schema(
[
StepSchema,
{
view: {
clickCount: {
type: Number,
default: 1,
min: 1,
},
},
},
],
{ discriminatorKey: `type` },
)
const Step = model(`Step`, StepSchema)
Step.discriminator(`click`, ClickSchema)
const doc = new Step({
type: `click`,
view: { url: `https://google.com` },
})
console.log('doc.view.url', doc.view.url) // prints undefined Do you think it is a bug or I am doing something wrong. If it is a bug and you want me to open it as a ticket, let me know ;) |
@vkarpov15 I saw you added the milestone 5.9.30 to this ticket but 5.10 is already released |
@gilles-yvetot the issue in this comment is that const doc = new mongoose.Document({
type: `click`,
view: { url: `https://google.com` },
}, StepSchema)
console.log('doc.view.url', doc.view.url) When we first wrote the browser library, we thought this approach made more sense because we don't currently support doing anything related to the database in the browser. However, we've thought about adding models to the browser library (see #4292). Please follow #4292 for updates. |
@vkarpov15 this comment refers to a bug I had in a NON-browser environment, i.e regular node environment. |
@vkarpov15 I opened this ticket instead of polluting this ticket |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
A mongoose browser document created using a discriminator schema can't access nested object properties from the parent schema, nor can it inherit virtuals from the base schema. Weirdly enough, it DOES gain access to them if I use
doc.get()
.If the current behavior is a bug, please provide the steps to reproduce.
https://github.com/varunjayaraman/mongoose-discriminator-bug
Here is a repo:
app.js
is the file that gets bundled up intodist/bundle.js
when you runnpm run compile
. Start the server up withnpm start
. Schemas are located inserver/schemas
.https://github.com/varunjayaraman/mongoose-discriminator-bug
What is the expected behavior?
Discriminator schema documents in the browser should be able to use subdocs that overlap with the parent/base schema.
Please mention your node.js, mongoose and MongoDB version.
Node: 6.9.2
Mongoose: 4.8.5
MongoDB: N/a but 3.4
The text was updated successfully, but these errors were encountered: