Nodejs- Req.body 在 post 中未定义,带有 express 4.x
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24755452/
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
Nodejs- Req.body undefined in post with express 4.x
提问by Ammar Khan
I am using a middleware body-parserto encoded the form values to get in req.body object.
But as I debug my code, found out req.body is undefined. Here is my code
我正在使用中间件body-parser对表单值进行编码以获取 req.body 对象。但是当我调试我的代码时,发现 req.body 是未定义的。这是我的代码
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
Listen Post request
听帖子请求
app.post('/newCategory', function (req,res) {
//express attached the form encoded values into body
var categoryName = req.body.categoryName;
});
Html Form
html表单
<form action="/newCategory" role="form" method="post" class="form-inline">
<input type="text" name="categoryName" placeholder="Category name" class="form-control" />
<input type="submit" value="New Category" class="btn btn-primary" />
</form>
回答by user3926346
Just ran into the same issue. It looks like I resolved my problem by moving my code to map routes after the urlencoded line. I am now seeing req.body in the post.
刚刚遇到了同样的问题。看起来我通过将代码移动到 urlencoded 行之后映射路由来解决我的问题。我现在在帖子中看到 req.body 。
app.use(bodyParser.urlencoded({ extended: true }));
// Map routes
var controllers = require("./controllers");
controllers.init(app);
回答by ackuser
This solved the problem for me
这为我解决了问题
var bodyParser = require('body-parser');
var app=express();
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
Hope this help
希望这有帮助
回答by Teodor
I noticed that the order is very important. Usually the router should be declared in the end before starting the server . Eg: 1.i import the required files
我注意到顺序非常重要。通常应该在最后启动服务器之前声明路由器。例如:1.i 导入所需文件
var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var db = require("./db.js");
var app = express();
2.i declare the other stuffs
2.我声明其他东西
app.set("port", process.env.PORT || 3000);
//app.use(express.static(__dirname + '/public'));
app.use(morgan('dev') ); // Log every request to console
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
3. AFTER I INCLUDE ROUTES - most important step
3. 在我包括路线之后 - 最重要的一步
var routes = require('./routes/routes');
routes(app); //routes shall use Express
start the server
app.listen(app.get("port"), function () { console.log("Express server listening on port " + app.get("port")); });
启动服务器
app.listen(app.get("port"), function () { console.log("Express 服务器监听端口" + app.get("port")); });
And then will work .. i'm not sure why but after it happen few time i learn the lesson .
然后会起作用..我不知道为什么,但在它发生几次之后我吸取了教训。
回答by Ben Fortune
If you're using urlencoded with { extended:false }, req.bodywill return the unparsed raw string from the form categoryName=test. Meaning req.body.categoryNamewill be undefined.
如果您使用 urlencoded with { extended:false },req.body将从表单中返回未解析的原始字符串categoryName=test。意义req.body.categoryName将是不确定的。
Try passing true so it can parse the form data using the qs module.
尝试传递 true 以便它可以使用 qs 模块解析表单数据。
app.use(bodyParser.urlencoded({
extended: true
}));
回答by Muhammad Soliman
As body-parser module is used to parse the body and urls, it should be called before any call to "req.body..."
由于 body-parser 模块用于解析正文和网址,因此应在调用“req.body...”之前调用它。
var bodyParser = require("body-parser");
///////req.body is undefined here
//extended: false means you are parsing strings only (not parsing images/videos..etc)
app.use(bodyParser.urlencoded({extended: false});
///////you req.body is working here (module below is using req.body)
app.use("/", module);
app.post('/newCategory', function (req,res) {
var categoryName = req.body.categoryName;
});
回答by Wanderson Elias
I am use this:
我正在使用这个:
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());but verify if your POSTMAN, for he can be the problem
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());但请确认您的邮递员,因为他可能是问题所在

