Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting req.param undefined

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.

like image 298
pyprism Avatar asked May 08 '14 17:05

pyprism


2 Answers

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.

like image 153
nowk Avatar answered Oct 10 '22 04:10

nowk


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

like image 32
mscdex Avatar answered Oct 10 '22 05:10

mscdex