正文解析器的req.body返回未定义的

时间:2016-06-05 16:42:14

标签: javascript node.js express

undefined持续获得req.body。为什么呢?

var express = require('express'),
    app = require('express')(),
    bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/', function (req, res) {
    console.log(req.body.username)
});

编辑:上面的代码片段是一个简化版本,我认为我包含了所有涉及的内容,但运行正常。以下是整个代码:https://jsfiddle.net/dx9kswdy/

3 个答案:

答案 0 :(得分:0)

仅打印输出req,如果它仍未定义,则传递给函数时未分配值。如果它返回一个JSON文件,则将其解析为一个对象!

app.post('/', function (req, res) {
    console.log(JSON.parse(req).body.username);
});

答案 1 :(得分:0)

当我启动你的例子时,它为我记录了身体的用户名

curl -X POST -d '{"username": "doug"}' -H "content-type: application/json" localhost:3000

dougwade npm-experiments/express-undef » node index.js doug

你得到什么症状?这是一个实际错误,还是只是记录" undefined"?如果您只是记录未定义,则问题是您的(正确解析的)正文上找不到密钥用户名。如果您收到错误,请发布完整的跟踪。

编辑:拉动小提琴然后跑了。不得不删除一些相对导入,但你使用body-parser就好了:

dougwade npm-experiments/express-undef » node index.js doug ReferenceError: user is not defined at /Users/doug.wade/npm-experiments/express-undef/index.js:23:9 at Layer.handle [as handle_request] (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/layer.js:95:5) at next (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/layer.js:95:5) at /Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:277:22 at Function.process_params (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:330:12) at next (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:271:10) at /Users/doug.wade/npm-experiments/express-undef/node_modules/body-parser/lib/read.js:129:5 at invokeCallback (/Users/doug.wade/npm-experiments/express-undef/node_modules/raw-body/index.js:262:16)

来自请求:

dougwade npm-experiments/express-undef » curl -X POST -d '{"username": "doug"}' -H "content-type: application/json" localhost:3000 ReferenceError: user is not defined<br> &nbsp; &nbsp;at /Users/doug.wade/npm-experiments/express-undef/index.js:23:9<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at next (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/route.js:131:13)<br> &nbsp; &nbsp;at Route.dispatch (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at /Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:277:22<br> &nbsp; &nbsp;at Function.process_params (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:330:12)<br> &nbsp; &nbsp;at next (/Users/doug.wade/npm-experiments/express-undef/node_modules/express/lib/router/index.js:271:10)<br> &nbsp; &nbsp;at /Users/doug.wade/npm-experiments/express-undef/node_modules/body-parser/lib/read.js:129:5<br> &nbsp; &nbsp;at invokeCallback (/Users/doug.wade/npm-experiments/express-undef/node_modules/raw-body/index.js:262:16)

答案 2 :(得分:0)

我已经遇到过这种情况了,这就是它通常最终的结果:

当您使用curl /浏览器发出请求时,请确保将Content-Type标头设置为application/json。这就是触发Express / Express正文解析器将字符串解析为JSON的原因。