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

Inheritance via discriminator, hooks are not inherited, contrary to the documentation #5147

Closed
qqilihq opened this issue Apr 5, 2017 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@qqilihq
Copy link

qqilihq commented Apr 5, 2017

Mongoose 4.9.3

I use schema inheritance, as described here. Samle code:

var options = {discriminatorKey: 'kind'};

var eventSchema = new mongoose.Schema({time: Date}, options);
eventSchema.pre('findOneAndUpdate', function() {
  console.log('eventSchema: findOneAndUpdate');
});
var Event = mongoose.model('Event', eventSchema);

var clickedEventSchema = new mongoose.Schema({url: String}, options)
clickedEventSchema.pre('findOneAndUpdate', function() {
  console.log('clickedEventSchema: findOneAndUpdate');
});
var ClickedLinkEvent = Event.discriminator('ClickedLink', clickedEventSchema);


mongoose.connect('mongodb://localhost:27017/test');

ClickedLinkEvent.findOneAndUpdate(
  { url: 'example.com' },
  { time: new Date() },
  { new: true, upsert: true },
  function(err, result) {
    console.log(err || result);
  }
);

eventSchema and clickedEventSchema each have their findOneAndUpdate hook.

UponfindOneAndUpdate, only the clickedEventSchema's hook is executed:

clickedEventSchema: findOneAndUpdate

This is contrary to what the documentation states:

Discriminators also take their base schema's pre and post middleware. However, you can also attach middleware to the discriminator schema without affecting the base schema.

Bug? Outdated documentation? Misunderstanding?

@sobafuchs
Copy link
Contributor

yeah this looks like a bug, thanks for reporting. Here's a full repro script to recreate:

const mongoose = require('mongoose');
const co = require('co');

mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-5147`;

exec()
  .then(() => {
    console.log('program ran successfully');
    process.exit(0);
  })
  .catch(error => {
    console.error(`Error: ${error}\n${error.stack}`);
    process.exit(2);
  });

function exec() {
  return co(function* () {
    const db = mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`);
    var options = { discriminatorKey: 'kind' };

    var eventSchema = new mongoose.Schema({ time: Date }, options);
    eventSchema.pre('findOneAndUpdate', function() {
      console.log('eventSchema: findOneAndUpdate'); // does not run
    });

    var Event = db.model('Event', eventSchema);

    var clickedEventSchema = new mongoose.Schema({ url: String }, options)
    clickedEventSchema.pre('findOneAndUpdate', function() {
      console.log('clickedEventSchema: findOneAndUpdate');
    });
    var ClickedLinkEvent = Event.discriminator('ClickedLink', clickedEventSchema);

    yield ClickedLinkEvent.remove({});
    const result = yield ClickedLinkEvent.findOneAndUpdate(
      { url: 'example.com' },
      { time: new Date() },
      { new: true, upsert: true });

    console.log('result', result);
  });
}

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 17, 2017
@sobafuchs sobafuchs added this to the 4.9.6 milestone Apr 17, 2017
vkarpov15 added a commit that referenced this issue Apr 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants