Golang RESTAPI 返回错误的数据

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

Golang RESTAPI returns wrong data

问题

当我使用调试器时,"metricId"返回的Metric对象的所有值都是0或null(与输出相同)。

我在这里做错了什么?

数据库连接是正常的。

func GetMetricById(c *gin.Context) {
    metricId := c.Param("id")

    conn := config.DatabaseConnect()

    var obj models.Metric

    rows := conn.QueryRow("SELECT * FROM Metric WHERE id = ?", metricId)

    err := rows.Scan(&obj.Id, &obj.Sms, &obj.Absence, &obj.Pregnant, &obj.Uwv_notifications, &obj.Wia_dossiers_started, &obj.Employees, &obj.Data_id, &obj.Date, &obj.Type)

    if err != nil {
        fmt.Println(err.Error())
    }

    fmt.Println(obj, "Metric Object that is called.")

    c.JSON(http.StatusOK, obj)
}

输出:

{
    "Id": 0,
    "Sms": 0,
    "Absence": 0,
    "Pregnant": 0,
    "Uwv_notifications": 0,
    "Wia_dossiers_started": 0,
    "Employees": 0,
    "Data_id": 0,
    "Date": "",
    "Type": 0
}
英文:

When i use the debugger the metricId returns the Metric Object with all 0 or null values (same as the output).

what am i doing wrong here?

The Database connection works.

func GetMetricById(c *gin.Context) {
	metricId := c.Param("id")

	conn := config.DatabaseConnect()

	var obj models.Metric

	rows := conn.QueryRow("SELECT * FROM Metric WHERE id = ?", metricId)

	err := rows.Scan(&obj.Id, &obj.Sms, &obj.Absence, &obj.Pregnant, &obj.Uwv_notifications, &obj.Wia_dossiers_started, &obj.Employees, &obj.Data_id, &obj.Date, &obj.Type)

	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Println(obj, "Metric Object that is called.")

	c.JSON(http.StatusOK, obj)
}

OUTPUT:

{
    "Id": 0,
    "Sms": 0,
    "Absence": 0,
    "Pregnant": 0,
    "Uwv_notifications": 0,
    "Wia_dossiers_started": 0,
    "Employees": 0,
    "Data_id": 0,
    "Date": "",
    "Type": 0
}

答案1

得分: 1

我认为解决方案可能是使用metricId := c.Params.ByName("id")而不是metricId := c.Param("id")

英文:

I think the solution may be to use metricId := c.Params.ByName("id") instead of metricId := c.Param("id")

答案2

得分: 1

func GetMetricsByDataId(c *gin.Context) {
	queryParams := c.Request.URL.Query()

	conn := config.DatabaseConnect()
	var metrObject []models.Metric

	rows, _ := conn.Query("SELECT * FROM Metric WHERE data_id = ?", queryParams.Get("data_id"))

	defer rows.Next()

	for rows.Next() {
		var obj models.Metric
		if err := rows.Scan(&obj.Id, &obj.Sms, &obj.Absence, &obj.Pregnant, &obj.Uwv_notifications, &obj.Wia_dossiers_started, &obj.Employees, &obj.Data_id, &obj.Date, &obj.Type); err != nil {
			_ = fmt.Errorf(err.Error())
		}
		metrObject = append(metrObject, obj)
	}
	if err := rows.Err(); err != nil {
		_ = fmt.Errorf(err.Error())
	}

	c.JSON(http.StatusOK, metrObject)
}

像这样它可以工作

localhost:8082/billing/getmetricbydataid?data_id=150295

通过这个API调用

英文:

[ANSWER]

func GetMetricsByDataId(c *gin.Context) {
	queryParams := c.Request.URL.Query()
	
	conn := config.DatabaseConnect()
	var metrObject []models.Metric

	rows, _ := conn.Query("SELECT * FROM Metric WHERE data_id = ?", queryParams.Get("data_id"))

	defer rows.Next()

	for rows.Next() {
		var obj models.Metric
		if err := rows.Scan(&obj.Id, &obj.Sms, &obj.Absence, &obj.Pregnant, &obj.Uwv_notifications, &obj.Wia_dossiers_started, &obj.Employees, &obj.Data_id, &obj.Date, &obj.Type); err != nil {
			_ = fmt.Errorf(err.Error())
		}
		metrObject = append(metrObject, obj)
	}
	if err := rows.Err(); err != nil {
		_ = fmt.Errorf(err.Error())
	}

	c.JSON(http.StatusOK, metrObject)
}

Like this it works

localhost:8082/billing/getmetricbydataid?data_id=150295

With this API call

huangapple
  • 本文由 发表于 2022年6月28日 17:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/72783661.html
匿名

发表评论

匿名网友

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

确定