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.

[question] Accessing 'req' object in a mongoose middleware

See original GitHub issue

Can I somehow add req.user._id to document before saving it.

Something like:

schema.pre('save', function (next) {
  this.createdBy = req.user._id;   // is it possible to access req here?
  next()
})

Do you know if it is possible to achieve something like that?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
Zertzcommented, Apr 18, 2016

My highly opiniated view on this is that you should not use req in your models as it couples your database with your web framework so I would recommend using the method I outlined above.

That said, we could investigate passing req into save. However, I’m curious as to what happens in the case of async middleware.

1reaction
Zertzcommented, Apr 15, 2016

Mongoose middleware doesn’t have access to the request object, it is also a separate project for which you can find documentation in their repository.

What you can do, is use our preCreate middleware.

restify.serve(router, model, {
  preCreate: function (req, res, next) {
    req.body.createdBy = req.user._id
    next()
  }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose pass req object to middleware - Stack Overflow
What i am trying to accomplish is to get the req.user role and the model name pass it to another function to get...
Read more >
Mongoose v6.8.2: Middleware
Query middleware is supported for the following Query functions. Query middleware executes when you call exec() or then() on a Query object, or...
Read more >
Middleware in Express.js - GeeksforGeeks
Middleware has the access to the request object, responses object, and next, it can process the request before the server send a response....
Read more >
Express/Node introduction - Learn web development | MDN
The middleware can perform any operation, execute any code, make changes to the request and response object, and it can also end the...
Read more >
How To Use the req Object in Express - DigitalOcean
Express servers receive data from the client side through the req object in three instances: the req.params , req.query , and req.body objects....
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