go - 使用 GORM 和 echo-framework 进行更新的惯用方式

标签 go go-gorm

您好,刚接触 golang,正在尝试制作 REST API

我想使用 GORM 和 golang echo 框架更新仅传递所需数据的实体。 我收到此错误: “strconv.ParseUint:解析\“ea78944c-a2a9-4813-86e7-10b199d0f002\”:语法无效”

我使用 echo.Bind() 函数将 formdata(我使用 postman)与我的 Club 结构绑定(bind)。

PS:我使用 xid 作为外部 id,并保留我的 int id 以进行内部工作。

预期:

我将使用 GORM 通过 id(通过 url 参数获取)在我的数据库中找到 Club,并将其设置在一个新的俱乐部结构中。

之后,我将我的俱乐部结构与我的表单数据绑定(bind)。

最后使用 Save() GORM 函数保存它。 => 出现错误

现实:

我将使用 GORM 通过 id(编写硬编码字符串 id)在我的数据库中找到 Club,并将其设置在一个新的俱乐部结构中。

之后,我将我的俱乐部结构与我的表单数据绑定(bind)。

最后使用 Save() GORM 函数保存它。 => 有效

结论:

我不知道如何使用 echo.Bind() 在 PUT 方法中传递 url 参数和表单数据

这是我的俱乐部结构:

// Club struct
type Club struct {
    Base
    Name    string  `json:"name;" gorm:"type:varchar(255);not null" validate:"required"`
    Slug    string  `json:"slug;" gorm:"type:varchar(255);unique;not null"`
    Website string  `json:"website;" gorm:"type:varchar(255)"`
    Lat     float32 `json:"lat;" gorm:"lat;" sql:"type:decimal(8,6);" validate:"required,numeric"`
    Lng     float32 `json:"lng;" gorm:"lng;" sql:"type:decimal(9,6);" validate:"required,numeric"`
    Logo    string  `json:"logo;" gorm:"type:varchar(100)" validate:"required"`
    Phone   string  `json:"phone;" gorm:"type:varchar(20)" validate:"required"`
}

我的 FindById 函数:

// GetClubByXID get club by xid
func GetClubByXID(c echo.Context, xid string) (*schemas.Club, error) {
    club := new(schemas.Club)
    if result := db.Where("xid = ?", xid).First(&club); result.Error != nil {
        return nil, result.Error
    }
    return club, nil
}

这是我的更新功能:

func UpdateClub(c echo.Context) error {
    xid := c.Param("id") // => doesn't work
        // xid := "ea78944c-a2a9-4813-86e7-10b199d0f002" // => work

    club, err := models.GetClubByXID(c, xid)
    if err != nil {
        return c.JSON(http.StatusNotFound, err)
    }

    if err := c.Bind(club); err != nil {
        return c.JSON(http.StatusInternalServerError, err)
    }

    db.Save(&club)
    return c.JSON(http.StatusOK, echo.Map{
        "club": &club,
    })
}

我的更新路线:

API.PUT("/updateClub/:id", handlers.UpdateClub) // => doesn't work
// API.PUT("/updateClub", handlers.UpdateClub) // => work

当我在更新函数中硬编码我的 xid ea78944c-a2a9-4813-86e7-10b199d0f002 时,它的工作就像一个魅力,但我无法将我的 url 和 formdata 与 echo.Bind 结合起来()

如果我尝试http://localhost:1323/api/updateClub/ea78944c-a2a9-4813-86e7-10b199d0f002,我会犯错误:

“strconv.ParseUint:正在解析\”ea78944c-a2a9-4813-86e7-10b199d0f002\“:语法无效”

感谢您的阅读,希望有人能帮助我:)

最佳答案

您能否尝试使用 c.Param("xid") 而不是 c.Param("id") 并将路由参数更改为 API。 PUT("/updateClub/:xid", handlers.UpdateClub)

关于go - 使用 GORM 和 echo-framework 进行更新的惯用方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662146/

相关文章:

templates - 使用 go 模板库访问特定的数组索引

postgresql - 使用 GORM 和 Postgresql 如何设置搜索路径?

go - 选择/查找模型时省略列

postgresql - 在两个不同的go应用程序中来自postgres的数据查询不一致

go - 如何修复 http : "panic serving 127.0.0.1:54063: 403 Forbidden: Forbidden"

memory - Go 如何在 make 或 new 调用中分配内存?

json - 使用 JSON 解码 map[string]int64 的 Go 中的问题

go - 避免Golang界面中的getter的解决方案

go - 预加载功能在gorm中有什么作用?

go - 带有两个外键的中间模型 : file structure?