获取req.param未定义 [英] Getting req.param undefined
本文介绍了获取req.param未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Expressjs版本4.我在req.param上得到未定义".这是我的例子: app.js
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
./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
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">
我也尝试过使用req.body
和req.params
,但是答案仍然相同.
I also tried with req.body
and req.params
, but the answer is still same.
推荐答案
req.params
引用路由路径中的变量.
req.params
Refers to the variables in your route path.
app.get("/posts/:id", ...
// => req.params.id
发布数据可以通过req.body
app.post("/posts", ...
// => req.body.email
这假定您正在使用bodyParser
中间件.
This assumes you are using the bodyParser
middleware.
然后是req.query
,对于那些?query=strings
.
您可以将req.param()
用于上面的3个.查找顺序为params
,body
,query
.
You can use req.param()
for either of the 3 above. The look up order is params
, body
, query
.
这篇关于获取req.param未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文