Skip to content
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

Closed
sobafuchs opened this issue Feb 28, 2017 · 12 comments
Closed

Comments

@sobafuchs
Copy link
Contributor

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 into dist/bundle.js when you run npm run compile. Start the server up with npm start. Schemas are located in server/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

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 28, 2017
@vkarpov15 vkarpov15 added this to the 4.8.7 milestone Mar 4, 2017
@vkarpov15
Copy link
Collaborator

This isn't really a bug. The schemas are only mutated server side, so childSchema doesn't actually have those fields on the client side. Unfortunately the current browser component has no real support for discriminators.

@vkarpov15 vkarpov15 removed this from the 4.8.7 milestone Mar 9, 2017
@gilles-yvetot
Copy link

gilles-yvetot commented Jul 22, 2020

@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?

@vkarpov15
Copy link
Collaborator

@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 Model.discriminator() won't work.

@gilles-yvetot
Copy link

@vkarpov15 but is schema inheritance not obtained from Model.discriminator()? How can you use schema inheritance in the browser then? Sorry to ask questions that might sound silly

@vkarpov15
Copy link
Collaborator

@gilles-yvetot can you please provide an example of how you're defining your discriminators?

@gilles-yvetot
Copy link

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)

@vkarpov15 vkarpov15 reopened this Aug 4, 2020
@vkarpov15 vkarpov15 added this to the 5.9.27 milestone Aug 4, 2020
@vkarpov15 vkarpov15 removed the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 4, 2020
@vkarpov15
Copy link
Collaborator

@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 vkarpov15 removed this from the 5.9.27 milestone Aug 5, 2020
@gilles-yvetot
Copy link

@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 vkarpov15 reopened this Aug 16, 2020
@vkarpov15 vkarpov15 added this to the 5.9.30 milestone Aug 16, 2020
@gilles-yvetot
Copy link

@vkarpov15 I saw you added the milestone 5.9.30 to this ticket but 5.10 is already released

@vkarpov15
Copy link
Collaborator

@gilles-yvetot the issue in this comment is that mongoose.model() is a no-op in the browser. Currently you need to do this instead:

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 vkarpov15 removed this from the 5.10.1 milestone Aug 25, 2020
@gilles-yvetot
Copy link

@vkarpov15 this comment refers to a bug I had in a NON-browser environment, i.e regular node environment.

@gilles-yvetot
Copy link

@vkarpov15 I opened this ticket instead of polluting this ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants