Getting req.param undefined

I am using Expressjs version 4. I get 'undefined' on req.param. Here is my example: app.js

var express = require('express'); var bodyParser = require('body-parser'); var newdata = require('./routes/new'); ........................ ...................... app.use(bodyParser()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded()); app.use('/new', newdata); 

./routes/new

 var express = require('express'); var router = express.Router(); router.get('/', function(req, res){ res.render('newdata', { title: 'Add new data' }) }); router.post('/', function(req, res){ console.log(req.param['email']); res.end(); }); module.exports = router; 

newdata.html

 <form action="/new" role="form" method="POST"> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" name="email" placeholder="Enter email"> 

I also tried with req.body and req.params , but the answer is still the same.

+8
source share
4 answers

req.params Refers to the variables in your route route.

 app.get("/posts/:id", ... // => req.params.id 

Mail data can be referenced via req.body

 app.post("/posts", ... // => req.body.email 

It is assumed that you are using bodyParser .

And then there is req.query , for those ?query=strings .


You can use req.param() for any of the above. The search order is params , body , query .

+30
source

param is a function, not an object. Therefore you need to use req.param('email');

+4
source

For those who encounter similar problems, be sure to use params instead of param.

 // Correct way req.params.ID // Wrong way req.param.ID 
+1
source

Two types of parameters are present
1. query (req.query. ('Name is defined in the route'));
2. path (req.params. (Name is defined in the route));

-1
source

All Articles