koajs / bodyparser

Koa body parsing middleware
MIT License
1.31k stars 117 forks source link

post request can't get the value #46

Closed houko closed 8 years ago

houko commented 8 years ago
//app.js
var  bodyParser = require('koa-bodyparser');
app.use(bodyParser());
//new.ejs
<!DOCTYPE html>
<html>
<head>
    <% include ./../common/header.ejs %>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading"><h1><%= title %></h1></div>
    <div class="panel-body">
        <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">
                <label for="nickname" class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="nickname" placeholder="用户名" value="<%= user.nickName %>">
                </div>
            </div>

            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="password" placeholder="密码" value="<%= user.password %>">
                </div>
            </div>

            <div class="form-group">
                <label for="phone" class="col-sm-2 control-label">电话</label>
                <div class="col-sm-10">
                    <input type="number" class="form-control" id="phone" placeholder="电话" value="<%= user.phone %>">
                </div>
            </div>

            <div class="form-group">
                <label for="imgUrl" class="col-sm-2 control-label">头像</label>
                <div class="col-sm-10">
                    <input type="url" class="form-control" id="imgUrl" placeholder="头像链接" value="<%= user.imgUrl %>">
                </div>
            </div>

            <div class="form-group">
                <label for="address" class="col-sm-2 control-label">地址</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="address" placeholder="地址" value="<%= user.address %>">
                </div>
            </div>

            <div class="form-group">
                <label for="gender" class="col-sm-2 control-label">性别</label>
                <div class="col-sm-10">
                    <select name="gender" id="gender" title="">
                        <option value="0" selected>请选择</option>
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </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>
    </div>
</div>

</body>
</html>
//user.model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

//创建模型
var UserSchema = new Schema({
    email: String,
    nickName: String,
    password: String,
    salt: String,
    gender: Number,
    phone: Number,
    imgUrl: String,
    address: String,
    createTime: Date,
    lastLogin: Date
});

//注册并导出
module.exports = mongoose.model('User', UserSchema);
user.router.js
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');
}
module.exports = router;

console.log(this.request.body) is {}

leenxyz commented 8 years ago

+1

dead-horse commented 8 years ago

大哥们你们敢不敢给一个用 curl 复现的例子?

leenxyz commented 8 years ago

@死马大哥 知道问题所在了,中间件顺序的问题

这样会导致 console.log(this.request.body) is {}

app
  .use(router.routes())
  .use(bodyParser())

@dead-horse

wohugb commented 7 years ago

在路由器后面,空了,连{}都没有了

leenxyz commented 7 years ago

@xiaomoinfo 我的意思就是 bodyParser 不能在 router 后面,请认真看~