【发布时间】:2021-03-04 03:51:39
【问题描述】:
我有一个非常简单的任务列表 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)
go-proj | (*DB).Updates: tx.callbacks.Update().Execute(tx)
go-proj | /app/app/controllers/listController.go:70 (0xb49c7b)
go-proj | Update: if err := models.DB.Model(&list).Updates(input).Error; err != nil {
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x93d01a)
go-proj | (*Context).Next: c.handlers[c.index](c)
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0x951004)
go-proj | RecoveryWithWriter.func1: c.Next()
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x93d01a)
go-proj | (*Context).Next: c.handlers[c.index](c)
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/logger.go:241 (0x950104)
go-proj | LoggerWithConfig.func1: c.Next()
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x93d01a)
go-proj | (*Context).Next: c.handlers[c.index](c)
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:409 (0x947359)
go-proj | (*Engine).handleHTTPRequest: c.Next()
go-proj | /go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:367 (0x946a4c)
go-proj | (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
go-proj | /usr/local/go/src/net/http/server.go:2843 (0x6cd7c2)
go-proj | serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
go-proj | /usr/local/go/src/net/http/server.go:1925 (0x6c8ecc)
go-proj | (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
go-proj | /usr/local/go/src/runtime/asm_amd64.s:1374 (0x46cba0)
go-proj | goexit: BYTE $0x90 // NOP
go-proj |
go-proj-nginx | 172.27.0.1 - - [20/Nov/2020:17:32:56 +0000] "PUT /list/3 HTTP/1.1" 500 0 "-" "PostmanRuntime/7.6.0"
我将提取我的代码中唯一重要的部分:
路由器:
r := gin.Default()
r.PUT("/list/:id", controllers.Update)
型号:
type List struct {
gorm.Model
UserId uint
Title string
Status uint
}
控制器:
type UpdateListInput struct {
Title string `json:"title"`
Status uint `json:"status"`
}
func Update(c *gin.Context) {
var list models.List
if err := models.DB.Where("id = ?", c.Param("id")).First(&list).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found"})
return
}
var input UpdateListInput
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := models.DB.Model(&list).Updates(input).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, gin.H{"data": list})
}
我将以下 JSON 发送到/list/:id:
{
"title": "Shopping List 2",
"status": 2
}
有关更多信息,您可以在 GitHub 上查看我的 repo 文件夹 .docker/,也许我在 Nginx、Postgres 甚至 Golang 配置上搞砸了,因为我在容器上运行我的应用程序。
编辑1: 我已经通过硬编码要在我的控制器中更新的数据来管理它:
func Update(c *gin.Context) {
var list models.List
if err := models.DB.Where("id = ?", c.Param("id")).First(&list).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found"})
return
}
input := map[string]interface{}{
"title": "AAAAAAAAAa",
"status": 3,
}
if err := models.DB.Model(&list).Updates(input).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": list})
}
【问题讨论】:
-
尝试从
c.Param("id")获取int值 -
@AlanSereb 也不起作用
标签: postgresql go go-gorm go-gin