question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

post request can't get the value

See original GitHub issue
//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 {}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
forsignercommented, Jul 30, 2016

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

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

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

@dead-horse

1reaction
wohugbcommented, Apr 12, 2017

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express POST request values not received - Stack Overflow
I'm trying to make a simple registration ...
Read more >
Sending form data - Learn web development | MDN
As we mentioned above, with a GET request the user will see the data in their URL bar, but with a POST request...
Read more >
GET Vs. POST: Key Difference Between HTTP Methods
GET method is used to appends form data to the URL in name or value pair. If you use GET, the length of...
Read more >
Difference between GET and POST request in Vanilla JavaScript
GET and POST is two different types of HTTP request methods. HTTP protocol supports many methods to ... You cannot bookmark POST requests....
Read more >
HTTP Requests - The PHP Framework For Web Artisans
To obtain an instance of the current HTTP request via dependency ... This value will be returned if the requested input value is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found