在koa中post请求中接收不到页面传到的值
1.在学习koa的时候用koa-generator生成的项目,里面自带了koa-bodyparser。但是我在使用post请求的时候发现页面传过来的值接收不到,去查了api也没找到什么问题。
相关代码
app.js
bodyParser = require('koa-bodyparser');
app.use(bodyParser());
new.ejs
<form class="form-horizontal" action="/users/create" method="post">
<div class="form-group">
<label for="email" class="col-sm-2 control-label">邮箱</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" placeholder="邮箱" value="<%= user.email %>">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">提交</button>
<a href="/users" class="btn btn-default">返回</a>
</div>
</div>
</form>
//users.js 路由
var router = require('koa-router')();
var mongoose = require('mongoose');
var User = mongoose.model('User');
router.get('/', list);
router.get('/new', add);
router.post('/create', save);
function *add(next) {
yield this.render('/users/new', {
title: '添加用户',
user: {
email: '',
nickName: '',
password: '',
gender: 0,
phone: 1,
imgUrl: '',
address: ''
}
});
}
function *save(next) {
console.log(this.request.body);
var user = this.request.body;
user.createTime = new Date;
user.lastLogin = new Date;
var id = users.push(user)
users.id = id - 1;
this.redirect('/users');
}
附上依赖
{
"name": "node-koa",
"version": "0.1.2",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"co": "^4.6.0",
"debug": "^2.2.0",
"ejs": "~2.3.3",
"koa": "^1.1.2",
"koa-bodyparser": "^2.2.0",
"koa-json": "^1.1.1",
"koa-logger": "^1.3.0",
"koa-mongoose": "^1.0.9",
"koa-onerror": "^1.2.1",
"koa-router": "^5.3.0",
"koa-static": "^1.5.2",
"koa-views": "^3.1.0",
"mongoose": "^4.4.19",
"uuid": "^2.0.2"
},
"devDependencies": {
"koa-webpack-dev-middleware": "^1.2.1",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
}
}
3.错误(点击看git动态图)
error
4.项目放在github上
node-koa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
try
你装的
koa-bodyparser
什么版本,装2.2.0
再试试1)<input type="email" class="form-control" name="email" id="email" placeholder="邮箱" value="<%= user.email %>">
2)检查一下node版本,看看是不是node版本过低