node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

accessing the matched object in a detail route

Open romain10009 opened this issue 10 years ago • 1 comments

Hi, so i wonder how to access to the matched item in this route for instance :

 .route('athirdroute', {
    handler: function(req, res, next) {
      res.locals.status_code = 200; // Store response status code
      res.locals.bundle = {
        athirdroute: "called" // And response data
      };
      next(); // Call *after* filters and then return the response
    },
    methods: ['get', 'post'],
    detail: true // Will mount the route at the detail endpoint /movies/:id/athirdroute
  })

how can i access to the movie that matched? I cannot find it on the req or res objects in the detail route. Would that "athirdroute" be triggered if there was no movie with the id ":id" ? Is the mongoose document available in res.local.bundle in the .after() hook? Thanks allot. Because id like not to have to query for the movie a second time for example if it is already available somewhere.

romain10009 avatar Oct 13 '15 00:10 romain10009

Hey there,

So. For custom routes, we don't do any preprocessing -- so, you can access the :id by doing req.params.id -- so you will have to query mongoose yourself to fetch the object.

In the after hook for custom routes, the only objects available in res.local.bundle are those that you put there in your middleware chain.

Part of the motivation for custom routes is that you can really do anything with them (they really only constrain you to RESTful routes, they don't add a ton of actual functionality). It might be interesting though / make sense for detail routes to fetch the object in question though....

baugarten avatar Nov 24 '15 15:11 baugarten