18

I am using Expressjs version 4.I am getting '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 same.

pyprism
  • 2,928
  • 10
  • 46
  • 85

5 Answers5

53

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

app.get("/posts/:id", ...

// => req.params.id

Post data can be referenced through req.body

app.post("/posts", ...

// => req.body.email

This assumes you are using the bodyParser middleware.

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


You can use req.param() for either of the 3 above. The look up order is params, body, query.

nowk
  • 32,822
  • 2
  • 35
  • 40
  • 2
    `req.param()` the function already checks `req.params` (the "variables in your route path"), `req.body`, and `req.query`. – mscdex May 08 '14 at 20:18
  • Hi , Suppose "/mobile/custom/******/deviceVersion/:deviceType" is my request . could you please tell me how to check "deviceType" against undefined? – Arj 1411 Jan 27 '17 at 07:25
  • Why `req.param()` is empty for me (using query string)? – Aleksey Kontsevich Aug 28 '17 at 21:55
  • This answer is super awesome ! POST worked for me with req.body.fieldName – Joseph Apr 07 '18 at 00:55
  • @nowk your solution is great, But When I have implemented it, I'm stuck with the error **Cannot read property 'productId' of undefined** here is the code `router.get('/:productId', (res, req, next )=>{ const id = req.params.productId if (id === 'gift-cards'){ res.send('you invoked Giftcards ID') } else { res.send(`You entered the ${id} ID`) } })` – Abdul R. Oct 01 '19 at 14:18
8

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

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 6
    That doesn't make any difference. – sahil shekhawat Feb 14 '16 at 17:09
  • I'm not sure why there is negative feedback as this solved my problem perfectly. I was following some old (or just plain wrong?) examples which used param like an object, but this guided me to the right path. – Jonty Morris Jul 17 '18 at 13:24
2

For anyone experiencing similar issues make sure to use params instead of param.

// Correct way
req.params.ID

// Wrong way
req.param.ID
College Code
  • 907
  • 6
  • 12
1

I also face the problem when I want to access req.params.id but I solved it by

index.js(main)

app.use('/addRoom', route);

router.js(router)

route.post('/:id',(req,res)=>{});

this works for me

-1

Two types are parameter are present
1. query (req.query.('name defined in route'));
2. path (req.params.('name defined in route));

Til
  • 5,150
  • 13
  • 26
  • 34
Abhinav Singh
  • 302
  • 3
  • 15