question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded document middleware doesn't get passed arguments on save

See original GitHub issue

Define 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:closed
  • Created 11 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
aheckmanncommented, Apr 17, 2012

work around: you can inspect the embedded documents parent in its pre by accessing its parent property.

a terrible hack prone to race conditions:

a = new newModel;
a._saveArg = 'hack';

embedded.pre('save', function (next) {
  console.log(this.parent._saveArg);
  next();
})
0reactions
wilsonpagecommented, Apr 18, 2012

Thanks a lot, for your help! Unless you think the feature I suggested is required, I don’t mind you closing this issue.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found