node.js 获取 req.param 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23548929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting req.param undefined
提问by pyprism
I am using Expressjs version 4.I am getting 'undefined' on req.param. Here is my example : app.js
我正在使用 Expressjs 版本 4.我在 req.param 上收到“未定义”。这是我的示例: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
新数据.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.bodyand req.params, but the answer is still same.
我也试过req.bodyand req.params,但答案仍然相同。
回答by nowk
req.paramsRefers to the variables in your route path.
req.params指路由路径中的变量。
app.get("/posts/:id", ...
// => req.params.id
Post data can be referenced through req.body
帖子数据可以通过 req.body
app.post("/posts", ...
// => req.body.email
This assumes you are using the bodyParsermiddleware.
这假设您正在使用bodyParser中间件。
And then there is req.query, for those ?query=strings.
然后是req.query那些?query=strings。
You can use req.param()for either of the 3 above. The look up order is params, body, query.
您可以req.param()用于上述 3 种中的任何一种。查找顺序是params, body, query。
回答by mscdex
回答by College Code
For anyone experiencing similar issues make sure to use paramsinstead of param.
对于遇到类似问题的任何人,请确保使用params而不是 param。
// Correct way
req.params.ID
// Wrong way
req.param.ID
回答by Abhinav Singh
Two types are parameter are present
1. query (req.query.('name defined in route'));
2. path (req.params.('name defined in route));
存在两种类型的参数
1. query (req.query.('name defined in route'));
2.路径(req.params.('路径中定义的名字));

