8

I am writing a middleware for mongoose that gets executed for every find object using pre query hook.

postSchema.pre('query', function(query, next) {
// I want to access the req.user object here
    query.populate('Category');
    next();
});

I want to access req.user object inside the pre for every request made to the api server. How can i pass the object to the middleware?

Is it even possible?

https://github.com/Automattic/mongoose/issues/1931

I found the above but it doesnt talk about passing req object.

====================

Edit after some confusion about the question.

What i am trying to accomplish is to get the req.user role and the model name pass it to another function to get the query condition for find. So depending on the user role and the type of model accessed the query condition will change.

Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
Uma Maheshwaraa
  • 563
  • 2
  • 9
  • 17

4 Answers4

0

Wrap the middleware in another middleware that has access to req.

Something like, assuming express

router.verb('/some-route', function (req, res, next) {
   postSchema.pre('query', function(query, next) {
      console.log(req);
      query.populate('Category');
      next();
   });
});

Edit - Attach this only to the route that you want the prehook for.

Disclaimer - Not tested.

Swaraj Giri
  • 4,007
  • 2
  • 27
  • 44
  • 1
    That would create an extra `pre` hook for every request (at least in Mongoose 4.x, although the `query` hook was replaced by `find` there; perhaps the semantics were changed as well). It'll also add the hook _after_ the model was created from the schema, which means the hook isn't registered to the model. – robertklep Jul 10 '15 at 07:10
  • His question was **Is it even possible?**. And the answer was, yep why the hell not. – Swaraj Giri Jul 10 '15 at 07:12
  • Yes, it will give him access to `req`. – Swaraj Giri Jul 10 '15 at 07:15
  • @SwarajGiri thanks for your response. I dont think the above construct will work because the query hook is not associated to the model. Moreover, this wont be scalable because for every route i need to add this pre for every model. I have 10+ models so a plugin approach would be the best. Apologies if i had asked the question wrong! What i am trying to accomplish is to get the req.user role and the model name pass it to another function to get the query condition for find. any other feasible approach please advise – Uma Maheshwaraa Jul 10 '15 at 13:44
  • 1
    Did anyone find a solution to this? I am trying to get the current logged in user in the pre find hook. – nacho Apr 13 '20 at 17:44
0

I know i'm joining this party late but you can use a service to pass the data you want between the request object and the mongoose prehook method. In your service create private variables that hold the data that you want to pass. Set those variables in your custom middleware and call the service get method to get the values in the mongoose prehook method.

ranman
  • 21
  • 1
  • 5
0

Use

  1. query.op to get the type of query
  2. query.options to get other options like {runValidators: true}
  3. query._condition to get the conditions of the query
  4. query._update to get the incoming body
  5. query._fields to get the selected fields,

You can also log the query to the terminal to see various options

0

Yes, it is possible.

restify.serve(router, model, {
   preCreate: function (req, res, next) {
       req.body.createdBy = req.user._id
       next()
   }
})

Follow this doc

Dhaval Italiya
  • 386
  • 2
  • 7