首頁 > 後端開發 > Golang > 主體

Gorm 建立表單資料檔上傳錯誤

PHPz
發布: 2024-02-09 22:06:08
轉載
890 人瀏覽過

Gorm 创建表单数据文件上传错误

php小編柚子今天為大家介紹的是關於Gorm建立表單資料檔上傳錯誤的問題。在開發過程中,我們經常會遇到文件上傳的需求,而Gorm是一個強大的ORM庫,它提供了方便的資料庫操作方法。然而,在使用Gorm進行表單資料檔案上傳時,有時會出現一些錯誤。本文將為大家解析這些錯誤,並提供對應的解決方案,幫助大家更好地應對這個問題。

問題內容

我正在嘗試在 postgresql 伺服器中建立記錄。該請求以多部分格式的文件資料形式發送給我。將檔案上傳到我這邊後,我呼叫 gorm.create,但它會拋出一個錯誤。

當我註解掉文件上傳部分時,錯誤消失,但我需要上傳文件。

這是我的控制器部分:

func (pc productcontroller) create(c *gin.context) {

    var product migrations.product

    if err := c.bind(&product); err != nil {
        c.json(400, gin.h{"error": err.error(), "message": "İşlem başarısız. lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-01"})
        return
    }

    if product.name == "" {
        c.json(400, gin.h{"error": "name is required", "message": "İşlem başarısız. lütfen ad alanını boş bırakmayınız. hata kodu: pd-crt-02"})
        return
    }

    if product.price == 0 {
        c.json(400, gin.h{"error": "price is required", "message": "İşlem başarısız. lütfen fiş değeri alanını boş bırakmayınız. hata kodu: pd-crt-03"})
        return
    }
    if product.id != 0 {
        c.json(400, gin.h{"error": "remove id field", "message": "lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-id-01"})
        return
    }

    file, err := c.formfile("image")
    if err != nil {
        c.json(400, gin.h{"error": err.error(), "message": "lütfen resim ekleyiniz. hata kodu: pd-crt-img-01"})
    }
    filename := time.now().format("20060102150405") + "-" + strings.split(file.filename, ".")[0] + "." + strings.split(file.filename, ".")[1]
    dst := fmt.sprintf("./public/images/%s", filename)
    err = c.saveuploadedfile(file, dst)
    if err != nil {
        c.json(400, gin.h{"error": err.error(), "message": "lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-img-02"})
        return
    }

    product.image = &migrations.file{
        path:      filename,
        extension: strings.split(file.filename, ".")[1],
    }
    log.println(product)
    err = db.conn.create(&product).error
    if err != nil {
        c.json(400, gin.h{"error": err.error(), "message": "İşlem başarısız. lütfen tekrar deneyiniz veya sistem yöneticinize başvurun. hata kodu: pd-crt-04"})
        return
    }

    c.json(http.statuscreated, gin.h{"message": "Ürün başarıyla eklendi.", "data": product})
    return
}
登入後複製

我的要求:

錯誤:

{
    "error": "strconv.parseint: parsing \"products\": invalid syntax; strconv.parseint: parsing \"products\": invalid syntax",
}
登入後複製

這是我的結構:

type Order struct {
    ID         uint       `gorm:"primarykey" json:"id"`
    UserID     int        `gorm:"index" json:"user_id"`
    RoomNo     int        `gorm:"comment:oda_no" json:"room_no"`
    IsDone     bool       `gorm:"default:false" json:"is_done"`
    StatusCode int        `gorm:"default:0" json:"status_code"`
    CreatedAt  time.Time  `json:"created_at"`
    UpdatedAt  time.Time  `json:"updated_at"`
    DeletedAt  time.Time  `gorm:"index" json:"deleted_at"`
    Products   []*Product `gorm:"many2many:orders_products" json:"products,omitempty"`
}

type Product struct {
    ID        uint      `gorm:"primarykey" json:"id" form:"id"`
    Name      string    `gorm:"type:varchar(255)" json:"name" form:"name"`
    Price     float64   `gorm:"type:decimal(10,2)" json:"price" form:"price"`
    IsActive  bool      `gorm:"default:true" json:"is_active" form:"isActive"`
    Image     File      `gorm:"polymorphic:Module" json:"image,omitempty"`
    CreatedAt time.Time `json:"created_at" form:"createdAt"`
    UpdatedAt time.Time `json:"updated_at" form:"updatedAt"`
    DeletedAt time.Time `gorm:"index" json:"deleted_at"`
}

type OrdersProduct struct {
    OrderID   int `gorm:"index" json:"order_id"`
    ProductID int `gorm:"index" json:"product_id"`
    Count     int `gorm:"default:0" json:"count"`
}


type File struct {
    ID         uint      `gorm:"primarykey" json:"id"`
    CreatedAt  time.Time `json:"created_at"`
    UpdatedAt  time.Time `json:"updated_at"`
    DeletedAt  time.Time `gorm:"index" json:"deleted_at"`
    Path       string    `gorm:"type:varchar(255)" json:"path"`
    Extension  string    `gorm:"type:varchar(255)" json:"extension"`
    ModuleID   int       `gorm:"type:integer" json:"module_id"`
    ModuleType int       `gorm:"type:integer" json:"module_type"`
}


登入後複製

解決方法

檢查檔案結構的單元類型。 strconv.ParseInt() 將字串轉換為值。我認為 ModuleID、ModuleType 或兩者都必須是字串。

以上是Gorm 建立表單資料檔上傳錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!