req.body.uname 不起作用是怎么回事?
发布于 9 年前 作者 Ireoo 4307 次浏览 最后一次编辑是 8 年前 来自 问答

错误代码: TypeError: Cannot read property ‘uname’ of undefined at /Users/SupermePole/Desktop/api_nodejs/app.js:21:63 at Layer.handle [as handle_request] (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/layer.js:95:5) at next (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/route.js:131:13) at next (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/route.js:125:14) at Route.dispatch (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/layer.js:95:5) at /Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/index.js:277:22 at Function.process_params (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/index.js:330:12) at next (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/router/index.js:271:10) at expressInit (/Users/SupermePole/Desktop/api_nodejs/node_modules/express/lib/middleware/init.js:33:5)

程序代码: app.post(’/’, function(req, res) { res.render(‘index’, { title: ‘Hey1’, message: req.body.uname } ); });

html:
    <input id="uname" name="uname" type="text" value="" />
5 回复

body 是 undefined

html不全,觉得是form的问题

post请求还是get请求

req.body 需要 bodyParser。你有没有加这个middleware?

@leapon 正解!

回到顶部