These are chat archives for gin-gonic/gin

16th
Aug 2017
David Nguyen
@thanhnvpt_twitter
Aug 16 2017 08:26
hi all
I've this model struct
type Product struct {
    Id          bson.ObjectId `bson:"_id,omitempty" json:"id"`
    CategoryId  bson.ObjectId `json:"categoryid"`
    Sku         string        `json:"sku"`
    Title       string        `json:"title" binding:"required"`
    Slug        string        `json:"slug"`
    Description string        `json:"description"`
    Quantity    int           `json:"quantity"`
    Price       float64       `json:"price"`
    Images      []string      `json:"images"`
    CreatedOn   time.Time     `json:"createdon"`
    CreatedBy   string        `json:"createdby"`
}
Bo-Yi Wu
@appleboy
Aug 16 2017 08:27
please format the struct.
David Nguyen
@thanhnvpt_twitter
Aug 16 2017 08:28
and at api i use
        productApi.POST("", func(c *gin.Context) {
            var product Product
            err = c.BindJSON(&product)
            fmt.Println(product)
            if err != nil {
                c.JSON(400, gin.H{
                    "error": err,
                    "data": "Not match product form",
                })
                return
            }

            // Auto create product slug from its Title
            product.Slug = slug.Make(product.Title)

            err = productCol.Insert(product)
            if err != nil {
                c.JSON(500, gin.H{
                    "error": err,
                    "data": nil,
                })
            }

            c.JSON(200, gin.H{
                "error": nil,
                "data": "Create product success",
            })
        })
But i alway got this
{
            "id": "599400c67ef89a6fad1a3cf0",
            "sku": "",
            "title": "",
            "slug": "",
            "description": "",
            "quantity": 0,
            "price": 0,
            "images": null,
            "createdon": "0001-01-01T00:00:00Z",
            "createdby": ""
        },
Every field is empty, please take a look
thanks
Bo-Yi Wu
@appleboy
Aug 16 2017 08:33
type Product struct {
    Id          bson.ObjectId `bson:"_id,omitempty" json:"id"`
    CategoryId  bson.ObjectId `bson:"xxxx" json:"categoryid"`
    Sku         string        `bson:"xxxx" json:"sku"`
    Title       string        `bson:"xxxx" json:"title" binding:"required"`
    Slug        string        `bson:"xxxx" json:"slug"`
    Description string        `bson:"xxxx" json:"description"`
    Quantity    int           `bson:"xxxx" json:"quantity"`
    Price       float64       `bson:"xxxx" json:"price"`
    Images      []string      `bson:"xxxx" json:"images"`
    CreatedOn   time.Time     `bson:"xxxx" json:"createdon"`
    CreatedBy   string        `bson:"xxxx" json:"createdby"`
}
You missing defined the bson field.
David Nguyen
@thanhnvpt_twitter
Aug 16 2017 08:35
many thanks @appleboy
Can you talk more why we need bson for all field
Bo-Yi Wu
@appleboy
Aug 16 2017 16:23
depened on your mongodb filed.
You can ignore bson:xxxx if you don’t need save to db.