仅在更新时出现 Gorm 和 Gin 错误 500

我有一个非常简单的任务列表 CRUD,到目前为止,我能够创建、列出所有、按 ID 列出和删除记录,但是当我尝试更新时,它给了我以下错误:

go-proj          | reflect: call of reflect.Value.Field on string Value

go-proj          | /usr/local/go/src/reflect/value.go:850 (0x4a2464)

go-proj          |      Value.Field: panic(&ValueError{"reflect.Value.Field", v.kind()})

go-proj          | /go/pkg/mod/gorm.io/gorm@v1.20.7/schema/field.go:393 (0x996e50)

go-proj          |      (*Field).setupValuerAndSetter.func2: fieldValue := reflect.Indirect(value).Field(field.StructField.Index[0]).Field(field.StructField.Index[1])

go-proj          | /go/pkg/mod/gorm.io/gorm@v1.20.7/callbacks/update.go:230 (0xb3e3f0)

go-proj          |      ConvertToAssignments: value, isZero := field.ValueOf(updatingValue)

go-proj          | /go/pkg/mod/gorm.io/gorm@v1.20.7/callbacks/update.go:64 (0xb3bfd9)

go-proj          |      Update: if set := ConvertToAssignments(db.Statement); len(set) != 0 {

go-proj          | /go/pkg/mod/gorm.io/gorm@v1.20.7/callbacks.go:105 (0x9a5b7c)

go-proj          |      (*processor).Execute: f(db)

go-proj          | /go/pkg/mod/gorm.io/gorm@v1.20.7/finisher_api.go:303 (0x9ad886)


我尝试过更改GormGin框架的版本

HUX布斯
浏览 269回答 2
2回答

呼唤远方

好吧,这是一种解决方法,可能我会在Gorm 的 Github上打开一个问题,因为我认为这不是正确的做法,我唯一要做的就是从使用反射UpdateListInput struct的变量转换包裹map[string]interface{}这是我的控制器:func Update(c *gin.Context) {&nbsp; &nbsp; &nbsp; &nbsp; var list models.List&nbsp; &nbsp; &nbsp; &nbsp; if err := models.DB.Where("id = ?", c.Param("id")).First(&list).Error; err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found"})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var input UpdateListInput&nbsp; &nbsp; &nbsp; &nbsp; if err := c.ShouldBindJSON(&input); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; v := reflect.ValueOf(input)&nbsp; &nbsp; &nbsp; &nbsp; typeOfV := v.Type()&nbsp; &nbsp; &nbsp; &nbsp; inputData := map[string]interface{}{}&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < v.NumField(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputData[typeOfV.Field(i).Name] = v.Field(i).Interface()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if err := models.DB.Model(&list).Updates(inputData).Error; err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; c.JSON(http.StatusOK, gin.H{"data": list})}

德玛西亚99

在控制器中试试这个:data := models.List{Title: input.Title,Status: input.Status}if err := models.DB.Model(&list).Updates(&data).Error; err != nil {&nbsp; &nbsp; &nbsp; &nbsp; c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})}c.JSON(http.StatusOK, gin.H{"data": data})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go