Returning array like json with Fiber, Golang

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

Returning array like json with Fiber, Golang

问题

我在我的数据库中有一个包含tokenuser_idlast_seen列的表格。

任务是在我的应用程序收到GET请求时将它们全部发送出去。

Fiber, Golang:

	type sessionsInfo struct {
		token     		string
		user_id      	int
		last_seen 		string
	}
	app.Get("/testA", func(c *fiber.Ctx) error {
		logMsg("GET /testA")
		var sessions sessionsInfo
		execQuery := `select * from sessions`
		rows, err := db.Query(execQuery)
		if err != nil {
			fmt.Println(err)
			return c.SendStatus(418)
		}
		var sessionsArray []sessionsInfo
		for rows.Next(){
			rows.Scan(
				&sessions.token,
				&sessions.user_id,
				&sessions.last_seen,
			)
			sessionsArray = append(sessionsArray, sessions)
		}
		defer rows.Close()
		fmt.Println(sessionsArray)
		c.JSON(sessionsArray)
		return c.SendStatus(200)
	})

奇怪的是,我在控制台中看到了正确的数组,完全匹配我的数据库中的数据。

[2021-11-04 15:50:31] GET /testA
[{be0e6163-ce2c-4ac3-5349-ff3b34a51c37 1 2021-10-21T17:50:42.308751Z}
 {6c4f21f1-dc00-4975-6fe2-c69d14328099 2 2021-10-21T17:51:27.963031Z}
 {824012ec-07d1-4732-63a1-7b1c99261c6d 3 2021-10-21T21:29:23.95517Z}
 {a13b67d8-370c-49d2-72da-21fd02081711 4 2021-10-22T07:45:43.539265Z}
 {ebb8fdfc-923b-4d8d-6988-781c71c17dbe 5 2021-10-26T08:48:50.12082Z}
 {212909aa-70c9-4f50-553c-0f503e65fa7b 6 2021-10-26T08:49:56.880621Z}
 {132fbbd0-5c81-4278-4e39-ed933872d58e 7 2021-10-26T10:07:26.221177Z}
 {753f278c-9a9f-4bd5-5a9c-dc4a14b6bc81 7 2021-10-26T11:11:34.955218Z}
 {c32dea1d-230c-4caa-50a8-0cb2bd97d562 7 2021-10-26T11:11:38.723956Z}
 {ea2a87e5-6140-40a2-6641-69e85c0fef34 7 2021-10-26T11:11:39.417712Z}
 {4fe4958a-ebfb-4c5e-61b0-5c6e7e9666b4 7 2021-10-26T11:11:40.018127Z}
 {feb7c3f0-26b0-47f9-63e1-d261e3093f30 7 2021-10-26T11:17:28.634217Z}
 {2a484f33-9b61-4ef1-7987-78aa07f6f6ff 7 2021-10-26T11:17:47.73872Z}
 {c9dbfe03-1435-4cae-67f9-1c0a010c14b6 7 2021-10-26T11:18:02.7325Z}
 {c866ca0c-0e16-486e-79cf-0a4b1b30c6ef 7 2021-10-26T11:18:03.160044Z}
 {aafc6dfc-0b91-41fa-6ebc-29053c097570 7 2021-10-26T11:18:03.680851Z}
 {d3325e13-7ec3-478b-6eb8-3b609440b0c8 8 2021-10-29T18:49:21.366545Z}
 {ef473f2e-66a2-4518-6e7e-7b0a21544444 7 2021-10-29T19:01:13.583646Z}
 {3aa6644b-815c-456d-5601-0ec5bb84c532 9 2021-10-29T19:03:53.866348Z}
 {6b8ecbea-dccf-4b90-4dd1-e5ca6f589560 10 2021-10-29T19:04:46.824083Z}
 {a39567bc-4001-4e50-45ff-638c08d4cb09 10 2021-10-29T19:12:30.415509Z}
 {e9a80c87-9d4f-4582-413f-f56210d313fd 10 2021-10-29T19:13:02.469866Z}
 {d9d15614-aeb1-4c74-75b2-b9f3f6fed9d2 11 2021-10-29T20:36:18.883786Z}
 {6398683b-94fa-4a7c-53a3-849084565ab7 11 2021-10-29T20:36:48.724322Z}
 {bed2a9fe-7288-41be-5786-0aa3710a4e22 11 2021-10-29T20:37:23.696741Z}]

但是出于某种原因,当我查看响应的主体时,我看到了一个包含25个空子JSON(我不知道如何正确命名它,哈哈),像这样:

[
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}
]

与应该有的数量相同,但是为空的。
结果应该是这样的:

[
    {
        "token": "be0e6163-ce2c-4ac3-5349-ff3b34a51c37",
        "id": "1",
        "last_seen": "2021-10-21T17:50:42.308751Z"
    },
    {
        "token": "6c4f21f1-dc00-4975-6fe2-c69d14328099",
        "id": "2",
        "last_seen": "2021-10-21T17:51:27.963031Z"
    }
    ...
    ...
    ...
]

我该如何修复这个问题?我做错了什么?

英文:

I have a table with columns token, user_id and last_seen in my databse

The task is to send all of them with my app when it gets a get request

Fiber, Golang:

	type sessionsInfo struct {
		token     		string
		user_id      	int
		last_seen 		string
	}
	app.Get("/testA", func(c *fiber.Ctx) error {
		logMsg("GET /testA")
		var sessions sessionsInfo
		execQuery := `select * from sessions`
		rows, err := db.Query(execQuery)
		if err != nil {
			fmt.Println(err)
			return c.SendStatus(418)
		}
		var sessionsArray []sessionsInfo
		for rows.Next(){
			rows.Scan(
				&sessions.token,
				&sessions.user_id,
				&sessions.last_seen,
			)
			sessionsArray = append(sessionsArray, sessions)
		}
		defer rows.Close()
		fmt.Println(sessionsArray)
		c.JSON(sessionsArray)
		return c.SendStatus(200)
	})

The strange thing is that i see correct array in console, matching 100% my data in DB

[2021-11-04 15:50:31] GET /testA
[{be0e6163-ce2c-4ac3-5349-ff3b34a51c37 1 2021-10-21T17:50:42.308751Z}
 {6c4f21f1-dc00-4975-6fe2-c69d14328099 2 2021-10-21T17:51:27.963031Z}
 {824012ec-07d1-4732-63a1-7b1c99261c6d 3 2021-10-21T21:29:23.95517Z}
 {a13b67d8-370c-49d2-72da-21fd02081711 4 2021-10-22T07:45:43.539265Z}
 {ebb8fdfc-923b-4d8d-6988-781c71c17dbe 5 2021-10-26T08:48:50.12082Z}
 {212909aa-70c9-4f50-553c-0f503e65fa7b 6 2021-10-26T08:49:56.880621Z}
 {132fbbd0-5c81-4278-4e39-ed933872d58e 7 2021-10-26T10:07:26.221177Z}
 {753f278c-9a9f-4bd5-5a9c-dc4a14b6bc81 7 2021-10-26T11:11:34.955218Z}
 {c32dea1d-230c-4caa-50a8-0cb2bd97d562 7 2021-10-26T11:11:38.723956Z}
 {ea2a87e5-6140-40a2-6641-69e85c0fef34 7 2021-10-26T11:11:39.417712Z}
 {4fe4958a-ebfb-4c5e-61b0-5c6e7e9666b4 7 2021-10-26T11:11:40.018127Z}
 {feb7c3f0-26b0-47f9-63e1-d261e3093f30 7 2021-10-26T11:17:28.634217Z}
 {2a484f33-9b61-4ef1-7987-78aa07f6f6ff 7 2021-10-26T11:17:47.73872Z}
 {c9dbfe03-1435-4cae-67f9-1c0a010c14b6 7 2021-10-26T11:18:02.7325Z}
 {c866ca0c-0e16-486e-79cf-0a4b1b30c6ef 7 2021-10-26T11:18:03.160044Z}
 {aafc6dfc-0b91-41fa-6ebc-29053c097570 7 2021-10-26T11:18:03.680851Z}
 {d3325e13-7ec3-478b-6eb8-3b609440b0c8 8 2021-10-29T18:49:21.366545Z}
 {ef473f2e-66a2-4518-6e7e-7b0a21544444 7 2021-10-29T19:01:13.583646Z}
 {3aa6644b-815c-456d-5601-0ec5bb84c532 9 2021-10-29T19:03:53.866348Z}
 {6b8ecbea-dccf-4b90-4dd1-e5ca6f589560 10 2021-10-29T19:04:46.824083Z}
 {a39567bc-4001-4e50-45ff-638c08d4cb09 10 2021-10-29T19:12:30.415509Z}
 {e9a80c87-9d4f-4582-413f-f56210d313fd 10 2021-10-29T19:13:02.469866Z}
 {d9d15614-aeb1-4c74-75b2-b9f3f6fed9d2 11 2021-10-29T20:36:18.883786Z}
 {6398683b-94fa-4a7c-53a3-849084565ab7 11 2021-10-29T20:36:48.724322Z}
 {bed2a9fe-7288-41be-5786-0aa3710a4e22 11 2021-10-29T20:37:23.696741Z}]

but with some reason when i look at answer's body i see json that has 25 empty sub-jsons (i dunno how to name it correctly, lmao), like this

[
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}
]

The same count as it should be, but empty.
The result should be something like this

[
    {
        "token": "be0e6163-ce2c-4ac3-5349-ff3b34a51c37",
        "id": "1",
        "last_seen": "2021-10-21T17:50:42.308751Z"
    },
    {
        "token": "6c4f21f1-dc00-4975-6fe2-c69d14328099",
        "id": "2",
        "last_seen": "2021-10-21T17:51:27.963031Z"
    }
    ...
    ...
    ...
]

How can i fix this? What am i doing wrong?

答案1

得分: 3

导出类型:

type sessionsInfo struct {
    token     string
    user_id   int
    last_seen string
}

转换为:

type SessionsInfo struct {
    Token     string `json:"token"`
    User_id   int    `json:"user_id"`
    Last_seen string `json:"last_seen"`
}
英文:

Export the types:

type sessionsInfo struct {
    token           string
    user_id         int
    last_seen       string
}

To:

type SessionsInfo struct {
    Token           string `json:"token"`
    User_id         int `json:"user_id"`
    Last_seen       string `json:"last_seen"`
}

huangapple
  • 本文由 发表于 2021年11月4日 21:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69839800.html
匿名

发表评论

匿名网友

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

确定