【发布时间】:2019-06-15 17:48:41
【问题描述】:
在昨天的整个下午和今天早上的几个小时之后,我一直遇到这个问题。
我更新了所有需要的包。 我使用 PostMan 发送 POST,使用 header Content-Type:application/json 和 body (Key=id) and (Value=32)(请查看下面的屏幕截图) .
使用的代码:
const express = require('express');
const bodyParser = require('body-parser');
const router = express.Router();
var app = express();
app.use(router);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var hostname = 'localhost';
var port = 3000;
app.listen(port, hostname, function(err) {
if (!err) {
console.log("Server OK at http://" + hostname + ":" + port);
}
});
router.post('/test', (req, res) => {
console.log('' + req.body.id);
});
我在 PostMap 上试过这个:
POST send from PostMan ; Second try from PostMan
我使用 Key=id Value=32 还有“id”和32 来自表单数据和其他三个(x-www-form-urlencoded,raw,...)
错误:
TypeError: 无法读取未定义的属性“id” 在 router.post (C:\SVDJS\test\simpleTest.js:19:30) 在 Layer.handle [as handle_request] (C:\SVDJS\node_modules\express\lib\router\layer.js:95:5) 在下一个 (C:\SVDJS\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (C:\SVDJS\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (C:\SVDJS\node_modules\express\lib\router\layer.js:95:5) 在 C:\SVDJS\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (C:\SVDJS\node_modules\express\lib\router\index.js:335:12) 在下一个 (C:\SVDJS\node_modules\express\lib\router\index.js:275:10) 在 Function.handle (C:\SVDJS\node_modules\express\lib\router\index.js:174:3) 在路由器 (C:\SVDJS\node_modules\express\lib\router\index.js:47:12)
我已经阅读了互联网上关于这个问题的大部分帖子,如果没有正当理由,请不要重定向。
如果有人可以帮助我,那就太好了。谢谢。
【问题讨论】:
-
我只想打个招呼,因为帖子似乎自动删除了它。
-
req.body未定义。尝试打印req -
您好,感谢您的回复。它显示 [object Object] :imgur.com/XU6nV8R 我也试过 console.log('' + JSON.stringify(req.body));但它显示未定义。
-
console.log(JSON.stringify(req))呢?
标签: node.js