在使用Go语言的gin和gorm发送请求时出现错误。

huangapple go评论88阅读模式
英文:

Error while sending request in Go with gin and gorm

问题

我最近开始学习Go,并决定使用gormgin创建一个简单的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,
	})
}

huangapple
  • 本文由 发表于 2023年2月25日 22:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75566195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定