Q:

req.params.id returning undefined when id is defined in separate file

When I use req.params.id with mongoose it returns undefined. I am fairly certain this is because I define the id parameter in my app.js and not my route file.
Route file:
//NEW - Form for adding new course
router.get("/new", function(req, res) {
    // find course by id
    Course.findById(req.params.id).exec(function(err, foundCourse) {
        if(err) {
            res.redirect("back");
        } else {
            //show template with correct id
            res.render("lectures/new", {course: foundCourse});
        }
    });
});

App.js file:
app.use("/courses/:id/lectures", lectureRoutes);

Is there any way to fix this so that I can access the id parameter from the route file?
EDIT:
the path I use in my browser:
http://localhost:7000/courses/5a4111286ae1111f541c3d88/lectures/new

A:

For this to work you have to enable the option called mergeParams.
const router = express.Router({ mergeParams: true });

See the documentation here.

