duanlisha2335 2016-04-25 04:34
浏览 49

无法在杜​​松子酒中使用mgo将表单数据插入数据库

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 
", 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条回答 默认 最新

  • doutangu4978 2016-04-25 05:02
    关注

    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"`
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法