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

Closed
kisshore04 opened this issue Apr 8, 2024 · 7 comments
Closed

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

kisshore04 opened this issue Apr 8, 2024 · 7 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.

@MaoShizhong
Copy link

MaoShizhong commented May 6, 2024

@kisshore04 as per the router.param documentation, the callback will only be triggered by route params defined within the router. Since department_id in your class-router.js is a param inherited through mergeParams: true, it won't trigger the .param callback.

In your other router, the department_id route param is defined within the router, so it triggers the .param callback.

@wesleytodd Would it be worth updating the documentation so the sentence before the linked one goes from

They are not inherited by mounted apps or routers.

to

They are not inherited by mounted apps or routers, nor are they triggered for route parameters inherited from parent routers.

@kisshore04
Copy link
Author

Yea thanks for clarifying!!

@wesleytodd
Copy link
Member

Yeah @MaoShizhong I think that would be a great addition to the docs! Those live over here if you want to open that pr: https://github.com/expressjs/expressjs.com

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

4 participants