Embedded document middleware doesn't get passed arguments on save
See original GitHub issueDefine Middlware:
embeddedSchema.pre('save', function(next) {
console.log(arguments);
next();
});
mainSchema.pre('save', function(next, myParam, callback) {
console.log(arguments);
next(callback);
});
Test Middleware
var newModel = new mainModel({
title: 'new-track'
});
newModel.save('my-first-param', function(err){
newModel.stars.push({ name: 'foo' });
newModel.save('my-second-param', function(err) {
console.log('DONE');
});
});
Log Result:
// The first mainSchema pre save middleware arguments
{
'0': [Function: fnWrapper],
'1': 'my-first-param', // <- Param passed into mainSchema save middleware
'2': [Function]
}
// The embeddedSchema pre save middleware arguments
{
'0': [Function: fnWrapper],
'1': [Function]
}
// The second mainSchema pre save middleware arguments
{
'0': [Function: fnWrapper],
'1': 'my-second-param', // <- Param passed into mainSchema save middleware
'2': [Function]
}
DONE
As you can see the main schema receives the correct arguments, but the embedded document middleware doesn’t. I need to pass arguments to the embedded document’s pre save middleware, how is this possible?
Issue Analytics
- State:
- Created 11 years ago
- Comments:7
Top Results From Across the Web
node / mongoose: getting to request-context ... - Stack Overflow
Hi, I am able to access req in middlewar pre save, but the document is not successfully saved. It returns undefined const savedItem...
Read more >Pre-save hooks in mongoose.js - Medium
It might be obvious, but a pre-save hook is middleware that is executed when a document is saved. Let's take a closer look...
Read more >mongoose - npm
Embedded documents enjoy all the same features as your models. Defaults, validators, middleware. Whenever an error occurs, it's bubbled to the save() error ......
Read more >Mongoose v6.8.1: Middleware
Pre middleware functions are executed one after another, when each middleware calls next . const schema = new Schema(..); schema.pre('save', ...
Read more >MongoDB | NestJS - A progressive Node.js framework
Think of it as the object you would normally pass as a second argument of the ... be implicitly reflected (for example, arrays...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
work around: you can inspect the embedded documents parent in its
pre
by accessing itsparent
property.a terrible hack prone to race conditions:
Thanks a lot, for your help! Unless you think the feature I suggested is required, I don’t mind you closing this issue.