I am trying to get value of path variable "userId" using req.params but i am getting undefined, if any one can guide me in this problem i ll be very thankful to him. i have place my code below.
i have go through some example but those examples are also doing in this way i don't know what is going wrong with my code.
thank you,
parent router for controller
app.use("/user/:userId/group",groupController);
Action In Controller
Router.post("/", function (req, res, next) {
var group = new Group(req.body);
console.log(req.params);
group.userId = req.params.userId;
group.save(new dataCallbacks(req, res, next, "Group").insert());
});
I think you are wrong with your route, you can't route to /user/:userId/group
and post to /
that doesn't make sense. I mean to get userId
param, you should post to /user/:userId/group
:
Route file route.js
:
var ctrl = require('controller.js');
app.route('/user/:userId/group').post(ctrl.doIt);
Controller file controller.js
:
exports.doIt = function(req, res, next) {
var group = new Group(req.body);
console.log(req.params);
group.userId = req.params.userId;
group.save(new dataCallbacks(req, res, next, "Group").insert());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With