mongodb - Cannot insert form data into database using mgo in gin -
i'm new go , using gin framework trying create user object:
const ( // collectionarticle holds name of users collection collectionuser = "users" ) // user table contains information 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 controller create user
// create user func create(c *gin.context) { db := c.mustget("db").(*mgo.database) //to 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 debugging log.printf("user is: %v", user ) log.printf("username is: %s , emails %s", user.username, user.email ) err = db.c(models.collectionuser).insert(user) if err != nil { c.error(err) } c.redirect(http.statusmovedpermanently, "/users") }
and 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 username of 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 terminal get:
[gin-debug] listening , serving http on :7000 [gin] 2016/04/25 - 06:30:04 | 200 | 549.499µs | 127.0.0.1 | /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: , emails
as can see username
, email
, password
field values passed controller. when check users
collection in the mongo database, see objects created fields submitted form empty. not figure out why happens, appreciate hints.
according gin document, need form
tag struct fields. like:
user string `form:"user" binding:"required"` password string `form:"password" binding:"required"`
Comments
Post a Comment