无法使用gin中的mgo将表单数据插入数据库。

huangapple go评论140阅读模式
英文:

Cannot insert form data into database using mgo in gin

问题

我是Go语言的新手,正在使用gin框架尝试创建一个用户对象:

  1. const (
  2. // CollectionArticle holds the name of the users collection
  3. CollectionUser = "users"
  4. )
  5. // User table contains the information for each user
  6. type User struct {
  7. ID bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
  8. Username string `json:"username" bson:"username"`
  9. Email string `json:"email" bson:"email"`
  10. Password string `json:"password" bson:"password"`
  11. StatusID uint8 `json:"status_id" bson:"status_id"`
  12. CreatedAt time.Time `json:"created_at" bson:"created_at"`
  13. UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
  14. Deleted uint8 `json:"deleted" bson:"deleted"`
  15. }

这是创建用户的控制器:

  1. // Create a user
  2. func Create(c *gin.Context) {
  3. db := c.MustGet("db").(*mgo.Database)
  4. //to help debugging
  5. x, _ := ioutil.ReadAll(c.Request.Body)
  6. log.Printf("request body is: %s \n", string(x))
  7. user := models.User{}
  8. err := c.Bind(&user)
  9. if err != nil {
  10. c.Error(err)
  11. return
  12. }
  13. //to help debugging
  14. log.Printf("user is: %v", user)
  15. log.Printf("username is: %s and emails is %s", user.Username, user.Email)
  16. err = db.C(models.CollectionUser).Insert(user)
  17. if err != nil {
  18. c.Error(err)
  19. }
  20. c.Redirect(http.StatusMovedPermanently, "/users")
  21. }

注册表单如下:

  1. <form action="/user/create" method="POST">
  2. <div class="form-group">
  3. <label for="username">Username</label>
  4. <input type="text" name="username" class="form-control" id="username" placeholder="Enter the username of the user">
  5. </div>
  6. <div class="form-group">
  7. <label for="email">Email</label>
  8. <input name="email" class="form-control" placeholder="Enter user email" />
  9. </div>
  10. <div class="form-group">
  11. <label for="password">Password</label>
  12. <input type="password" name="password" class="form-control" placeholder="Password" required>
  13. </div>
  14. <button type="submit" class="btn btn-default">Submit</button>
  15. </form>

在终端中,我得到了以下输出:

  1. [GIN-debug] Listening and serving HTTP on :7000
  2. [GIN] 2016/04/25 - 06:30:04 | 200 | 549.499µs | 127.0.0.1 | GET /register
  3. request body is: username=bob&email=bob%40me.com&password=1234
  4. user is: {ObjectIdHex("") 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0}
  5. username is: and emails is

如你所见,usernameemailpassword字段的值被传递给了控制器。当我在Mongo数据库中检查users集合时,我发现对象已经创建,但是从表单提交的字段为空。
我无法弄清楚为什么会发生这种情况,所以希望你能给我一些提示。

英文:

I'm new to Go and using gin framework trying to create a user object:

  1. const (
  2. // CollectionArticle holds the name of the users collection
  3. CollectionUser = &quot;users&quot;
  4. )
  5. // User table contains the information for each user
  6. type User struct {
  7. ID bson.ObjectId `json:&quot;_id,omitempty&quot; bson:&quot;_id,omitempty&quot;`
  8. Username string `json:&quot;username&quot; bson:&quot;username&quot;`
  9. Email string `json:&quot;email&quot; bson:&quot;email&quot;`
  10. Password string `json:&quot;password&quot; bson:&quot;password&quot;`
  11. StatusID uint8 `json:&quot;status_id&quot; bson:&quot;status_id&quot;`
  12. CreatedAt time.Time `json:&quot;created_at&quot; bson:&quot;created_at&quot;`
  13. UpdatedAt time.Time `json:&quot;updated_at&quot; bson:&quot;updated_at&quot;`
  14. Deleted uint8 `json:&quot;deleted&quot; bson:&quot;deleted&quot;`
  15. }

This is the controller to create user

  1. // Create a user
  2. func Create(c *gin.Context) {
  3. db := c.MustGet(&quot;db&quot;).(*mgo.Database)
  4. //to help debugging
  5. x, _ := ioutil.ReadAll(c.Request.Body)
  6. log.Printf(&quot;request body is: %s \n&quot;, string(x))
  7. user := models.User{}
  8. err := c.Bind(&amp;user)
  9. if err != nil {
  10. c.Error(err)
  11. return
  12. }
  13. //to help debugging
  14. log.Printf(&quot;user is: %v&quot;, user )
  15. log.Printf(&quot;username is: %s and emails is %s&quot;, user.Username, user.Email )
  16. err = db.C(models.CollectionUser).Insert(user)
  17. if err != nil {
  18. c.Error(err)
  19. }
  20. c.Redirect(http.StatusMovedPermanently, &quot;/users&quot;)
  21. }

And the registration form is:

  1. &lt;form action=&quot;/user/create&quot; method=&quot;POST&quot;&gt;
  2. &lt;div class=&quot;form-group&quot;&gt;
  3. &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
  4. &lt;input type=&quot;text&quot; name=&quot;username&quot; class=&quot;form-control&quot; id=&quot;username&quot; placeholder=&quot;Enter the username of the user&quot; &gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;form-group&quot;&gt;
  7. &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
  8. &lt;input name=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;Enter user email&quot; /&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;form-group&quot;&gt;
  11. &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
  12. &lt;input type=&quot;password&quot; name=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot; required&gt;
  13. &lt;/div&gt;
  14. &lt;button type=&quot;submit&quot; class=&quot;btn btn-default&quot;&gt;Submit&lt;/button&gt;
  15. &lt;/form&gt;

In the terminal I get:

  1. [GIN-debug] Listening and serving HTTP on :7000
  2. [GIN] 2016/04/25 - 06:30:04 | 200 | 549.499&#181;s | 127.0.0.1 | GET /register
  3. request body is: username=bob&amp;email=bob%40me.com&amp;password=1234
  4. user is: {ObjectIdHex(&quot;&quot;) 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0}
  5. 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标签来定义字段的结构,例如:

  1. User string `form:"user" binding:"required"`
  2. Password string `form:"password" binding:"required"`
英文:

according to the gin document, you need form tag to struct fields. like:

  1. User string `form:&quot;user&quot; binding:&quot;required&quot;`
  2. Password string `form:&quot;password&quot; binding:&quot;required&quot;`

huangapple
  • 本文由 发表于 2016年4月25日 12:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/36832072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定