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

Hooks not getting fired #5073

Closed
augusto-elevenapp opened this issue Mar 14, 2017 · 10 comments
Closed

Hooks not getting fired #5073

augusto-elevenapp opened this issue Mar 14, 2017 · 10 comments

Comments

@augusto-elevenapp
Copy link

Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Hooks are not firing
If the current behavior is a bug, please provide the steps to reproduce.

const newEntity = new Entity(req.body); newEntity.save()

FromSchema.pre('save', function(next) { console.log('pre save..'); next(); });

What is the expected behavior?

When I save the entity, I should see the log

Please mention your node.js, mongoose and MongoDB version.

Mongoose: 4.9.0
Node: 6.9.5
MongoDb 3.4.2

@augusto-elevenapp augusto-elevenapp changed the title Hooks not get fired Hooks not getting fired Mar 14, 2017
@chetverikov
Copy link
Contributor

+1

@chetverikov
Copy link
Contributor

I have determined that this problem appeared since version 4.8.0. Why hadn't anyone noticed it before?

@alolis
Copy link

alolis commented Mar 16, 2017

+1

Took me a while to figure out what's going on as well... I am using 4.8.3

@chetverikov
Copy link
Contributor

So, I'm researched this issue and i found trouble. It reproduce code:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
  name: String
});

const Entity = mongoose.model('Entity', schema); // create model before hooks

schema.pre('validate', function(next) {
  console.log('first pre validate...');
  next();
});

schema.pre('save', function(next) {
  console.log('first pre save...');
  next();
});

schema.pre('save', function(next) {
  console.log('second pre save...');
  next();
});

schema.post('save', function() {
  console.log('post save');
});

const entity = new Entity({name: 'Foo'});

mongoose.connect('mongodb://localhost/mongoose')
  .then(() => entity.save())
  .then(() => entity.set('name', 'Bar').save())
  .then(() => Entity.findOne(entity._id))
  .then(fromDb => fromDb.set('name', 'WAT').save())
  .then(() => mongoose.disconnect())
  .catch(reason => (console.log(reason), mongoose.disconnect()));

we can set hooks after create model in mongoose <4.8, but in mongoose >4.8 we cannot do it.
This is a breaking change and 4.8 should be called name 5.0

@chetverikov
Copy link
Contributor

@vkarpov15

@sobafuchs
Copy link
Contributor

@chetverikov this was never a supported behavior, we just enforced it after 4.8. Always set your hooks before calling mongoose.model()

@alolis
Copy link

alolis commented Mar 20, 2017

@varunjayaraman maybe it would be a good idea to clarify this in documentation regarding hooks or else someone might end up pulling his hair trying to figure out what's going on.

@blyork
Copy link

blyork commented May 30, 2017

Is there a workaround for this change of behavior? I currently have a separate module for my mongoose docs and apply the needed hooks for the specific app needs after the fact.

@vkarpov15
Copy link
Collaborator

@blyork unfortunately not. However, if you have a separate module, I'd recommend you have that module export schemas and then attach hooks to those schemas in your apps.

mernxl added a commit to mernxl/mongoose-id-assigner that referenced this issue May 11, 2019
@see Automattic/mongoose#5073

`pre-save` hooks not triggered if set after initialising model for mongoose >= 4.8.0, so all models whose Assigner was initialised using MongooseIdAssigner constructor will fail, but those with MongooseIdAssigner.plugin is ok.
mernxl added a commit to mernxl/mongoose-id-assigner that referenced this issue May 12, 2019
BREAKING CHANGE:
@see Automattic/mongoose#5073
MongooseIdAssigner WILL NOT BE initialised using Models.
Plugin can now only be used by calling `schema.plugin(MongooseIdAssigner, options)`
or `schema.plugin(MongooseIdAssigner.plugin, options)`

To get IdAssigner Instance,
1: Get from `localStateStore.getIdAssigner(schema)`
2: Use `const IA = MongooseIdAssigner.plugin(schema, options)`
3: Use `const IA = new MongooseIdAssigner(schema, options)`
mernxl added a commit to mernxl/mongoose-id-assigner that referenced this issue Dec 5, 2019
@see Automattic/mongoose#5073

`pre-save` hooks not triggered if set after initialising model for mongoose >= 4.8.0, so all models whose Assigner was initialised using MongooseIdAssigner constructor will fail, but those with MongooseIdAssigner.plugin is ok.
mernxl added a commit to mernxl/mongoose-id-assigner that referenced this issue Dec 5, 2019
BREAKING CHANGE:
@see Automattic/mongoose#5073
MongooseIdAssigner WILL NOT BE initialised using Models.
Plugin can now only be used by calling `schema.plugin(MongooseIdAssigner, options)`
or `schema.plugin(MongooseIdAssigner.plugin, options)`

To get IdAssigner Instance,
1: Get from `localStateStore.getIdAssigner(schema)`
2: Use `const IA = MongooseIdAssigner.plugin(schema, options)`
3: Use `const IA = new MongooseIdAssigner(schema, options)`
@sam13591980
Copy link

@chetverikov this was never a supported behavior, we just enforced it after 4.8. Always set your hooks before calling mongoose.model()

What is the point of that? It took me half a day to find such change!

@Automattic Automattic locked and limited conversation to collaborators Nov 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants