英文:
Cannot insert form data into database using mgo in gin
问题
我是Go语言的新手,正在使用gin框架尝试创建一个用户对象:
const (
// CollectionArticle holds the name of the users collection
CollectionUser = "users"
)
// User table contains the information for each user
type User struct {
ID bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
StatusID uint8 `json:"status_id" bson:"status_id"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
Deleted uint8 `json:"deleted" bson:"deleted"`
}
这是创建用户的控制器:
// Create a user
func Create(c *gin.Context) {
db := c.MustGet("db").(*mgo.Database)
//to help debugging
x, _ := ioutil.ReadAll(c.Request.Body)
log.Printf("request body is: %s \n", string(x))
user := models.User{}
err := c.Bind(&user)
if err != nil {
c.Error(err)
return
}
//to help debugging
log.Printf("user is: %v", user)
log.Printf("username is: %s and emails is %s", user.Username, user.Email)
err = db.C(models.CollectionUser).Insert(user)
if err != nil {
c.Error(err)
}
c.Redirect(http.StatusMovedPermanently, "/users")
}
注册表单如下:
<form action="/user/create" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" class="form-control" id="username" placeholder="Enter the username of the user">
</div>
<div class="form-group">
<label for="email">Email</label>
<input name="email" class="form-control" placeholder="Enter user email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
在终端中,我得到了以下输出:
[GIN-debug] Listening and serving HTTP on :7000
[GIN] 2016/04/25 - 06:30:04 | 200 | 549.499µs | 127.0.0.1 | GET /register
request body is: username=bob&email=bob%40me.com&password=1234
user is: {ObjectIdHex("") 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0}
username is: and emails is
如你所见,username
、email
和password
字段的值被传递给了控制器。当我在Mongo数据库中检查users
集合时,我发现对象已经创建,但是从表单提交的字段为空。
我无法弄清楚为什么会发生这种情况,所以希望你能给我一些提示。
英文:
I'm new to Go and using gin framework trying to create a user object:
const (
// CollectionArticle holds the name of the users collection
CollectionUser = "users"
)
// User table contains the information for each user
type User struct {
ID bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
StatusID uint8 `json:"status_id" bson:"status_id"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
Deleted uint8 `json:"deleted" bson:"deleted"`
}
This is the controller to create user
// Create a user
func Create(c *gin.Context) {
db := c.MustGet("db").(*mgo.Database)
//to help debugging
x, _ := ioutil.ReadAll(c.Request.Body)
log.Printf("request body is: %s \n", string(x))
user := models.User{}
err := c.Bind(&user)
if err != nil {
c.Error(err)
return
}
//to help debugging
log.Printf("user is: %v", user )
log.Printf("username is: %s and emails is %s", user.Username, user.Email )
err = db.C(models.CollectionUser).Insert(user)
if err != nil {
c.Error(err)
}
c.Redirect(http.StatusMovedPermanently, "/users")
}
And the registration form is:
<form action="/user/create" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" name="username" class="form-control" id="username" placeholder="Enter the username of the user" >
</div>
<div class="form-group">
<label for="email">Email</label>
<input name="email" class="form-control" placeholder="Enter user email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
In the terminal I get:
[GIN-debug] Listening and serving HTTP on :7000
[GIN] 2016/04/25 - 06:30:04 | 200 | 549.499µs | 127.0.0.1 | GET /register
request body is: username=bob&email=bob%40me.com&password=1234
user is: {ObjectIdHex("") 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0}
username is: and emails is
AS you can see username
, email
and password
field values are passed to the controller. When I check users
collection in the the mongo database, I see that the objects are created but the fields submitted from the form are empty.
I could not figure out why this happens, so appreciate your hints.
答案1
得分: 2
根据gin文档,你需要使用form
标签来定义字段的结构,例如:
User string `form:"user" binding:"required"`
Password string `form:"password" binding:"required"`
英文:
according to the gin document, you need form
tag to struct fields. like:
User string `form:"user" binding:"required"`
Password string `form:"password" binding:"required"`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论