문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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

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

// Correct way
req.params.ID

// Wrong way
req.param.ID

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top