0

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

任务是在收到获取请求时将它们与我的应用程序一起发送

光纤,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)
    })

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

[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 的 json(我不知道如何正确命名它,lmao),就像这样

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

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

[
    {
        "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"
    }
    ...
    ...
    ...
]

我怎样才能解决这个问题?我究竟做错了什么?

4

1 回答 1

0

导出类型:

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"`
}
于 2022-02-10T05:08:32.430 回答