英文:
Error while sending request in Go with gin and gorm
问题
我最近开始学习Go,并决定使用gorm
和gin
创建一个简单的CRUD后端。我已经实现了以下函数来检索查询结果。
我无法使一些端点正常工作。
func GetCurrentStock(c *gin.Context) {
result := initializers.DB.Table("dev.inventory_live").Debug().Select("sum(qtynos)").Where("NOT is_cut AND (remarks IS NULL OR remarks = '')")
c.JSON(http.StatusOK, gin.H{
"result": result,
})
}
当我发送请求到这个端点时,我一直收到这个错误
Error #01: json: unsupported type: func() time.Time
我是否遗漏了什么?我一直没有找到解决方案。
英文:
I started Go very recently and decided to create a simple CRUD back-end using gorm
and gin
.I have implemented the following function to retrieve a query result.
I can't get some of the endpoints to work.
func GetCurrentStock(c *gin.Context) {
result := initializers.DB.Table("dev.inventory_live").Debug().Select("sum(qtynos)").Where("NOT is_cut AND (remarks IS NULL OR remarks = '')")
c.JSON(http.StatusOK, gin.H{
"result": result,
})
}
When I send a request to the end point, I keep getting this error
Error #01: json: unsupported type: func() time.Time
Is there something I am missing? I haven't been able to find a solution
答案1
得分: 1
使用@mkopriva提供的输入和我找到的另一个问题,我意识到我错误地检索了结果。正确的方法是:
func GetCurrentStock(c *gin.Context) {
var result int
initializers.DB.Table("dev.inventory_live").Select(fmt.Sprintf("sum(%v)", dim)).Where("NOT is_cut AND (remarks IS NULL OR remarks = '')").Scan(&result)
c.JSON(http.StatusOK, gin.H{
"result": result,
})
}
英文:
Using the input @mkopriva gave and another question I found, I realised I was incorrectly retrieving the result. The right way is:
func GetCurrentStock(c *gin.Context) {
var result int
initializers.DB.Table("dev.inventory_live").Select(fmt.Sprintf("sum(%v)", dim)).Where("NOT is_cut AND (remarks IS NULL OR remarks = '')").Scan(&result)
c.JSON(http.StatusOK, gin.H{
"result": result,
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论