Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a clarification on router.param() usage. #5586

Open
kisshore04 opened this issue Apr 8, 2024 · 4 comments
Open

Need a clarification on router.param() usage. #5586

kisshore04 opened this issue Apr 8, 2024 · 4 comments

Comments

@kisshore04
Copy link

I was using router.param() method to run middleware in all the routes which has ':department_id'.
for example:
In my app, the route will be app.use('/department/:department_id/class',departmentRoutes);
In my departmentRoutes, child route will be router.get('/:id', ...controller);

I have use { mergeParams : true } to include all the params in the router to consider.. but when i use router.param(':department_id', ); the middleware gets skipped as it considers the ':department_id' as no parameter.

But the when we use router.param('id',); the middleware logic gets executed perfectly!!

kindly clear me of the doubt that, will the router.param() consider the params from the parent or app.

@wesleytodd
Copy link
Member

Hi @kisshore04, can you post more clear code examples? Ideally a single file/code block which illustrates your setup in its entirety? These partial lines do not help anyone answer your question. If it turns out this is a bug or in need of better docs or something I will leave this open awaiting more information, but we do not provide technical support so if that is what you are looking for you might have better luck on StackOverflow or Reddit.

@joeyguerra
Copy link

@kisshore04 have you tried router.param(‘department_id’)? Without the colon. https://expressjs.com/en/4x/api.html

@kisshore04
Copy link
Author

@kisshore04 have you tried router.param(‘department_id’)? Without the colon. https://expressjs.com/en/4x/api.html

yeah i have always used router.param('department_id'); without the colon. I mistyped it in here.

@kisshore04
Copy link
Author

Hi @kisshore04, can you post more clear code examples? Ideally a single file/code block which illustrates your setup in its entirety? These partial lines do not help anyone answer your question. If it turns out this is a bug or in need of better docs or something I will leave this open awaiting more information, but we do not provide technical support so if that is what you are looking for you might have better luck on StackOverflow or Reddit.

I apologize for the confusion in my previous message. Here is the revised version of the issue. I'm encountering an issue with the router.param function in Express.js. I have a setup where I'm expecting a callback function (myCallback) to be executed when there is a request to the route /api/department/:department_id/class/:id. However, during testing, I found that myCallback is not being invoked.

Here's the relevant code from app.js:

const express = require("express")
const app = express()

app.use("/api/department/:department_id/class", classRouter);
app.use("/api/department/", departmentRouter);


And from class-router.js:

const express = require("express")
const router = express.Router({mergeParams: true})

router.param("department_id", myCallback);

router.get("/:id", controllerFuntion)

Interestingly, in my department router where I have a similar setup, the middleware is executing successfully. The route to this is: /api/department/:department_id.

const express = require("express")
const router = express.Router({mergeParams: true})

router.param("department_id", myCallback);

router.get("/:department_id", getDeptDetails)

I've used the {mergeParams: true} option while creating the router and I have not used the colon (:) when I call the router.param() function.

Any help on why myCallback is not running when the request is coming to the nested route would be greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants