程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 Go 中使用 AppEngine 从 Firestore 读取不一致大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 Go 中使用 AppENGIne 从 Firestore 读取不一致?

开发过程中遇到在 Go 中使用 AppENGIne 从 Firestore 读取不一致的问题如何解决?下面主要结合日常开发的经验,给出你关于在 Go 中使用 AppENGIne 从 Firestore 读取不一致的解决方法建议,希望对你解决在 Go 中使用 AppENGIne 从 Firestore 读取不一致有所启发或帮助;

我有一个用 Go 编写并部署到 Google AppENGIne 的后端。它使用 Firestore 作为数据库。路由器是 chi。

当我现在尝试阅读模型中最重要的部分时,我得到了不一致的结果。 它在 nulltrue 之间切换。我已经检查了 Firebase 控制台。它绝对设置为 true 并且不会切换。

在 Go 中使用 AppEngine 从 Firestore 读取不一致

这是定义相关字段的模型:

type transactionModel struct {
    // ...
    Succeeded          bool   `Json:"succeeded,omitempty" firestore:"succeeded"`
}

这是处理请求的服务器函数:


func (s *servicE) getPaymentData(w http.ResponseWriter,r *http.request) {
    slug := chi.URLParam(r,"slug")
    ID := chi.URLParam(r,"ID")

    doc,err := s.db.Collection("payment/" + slug + "/transactions").Doc(ID).Get(r.Context())
    if err != nil {
        w.Writeheader(http.StatusnotFound)
        logrus.WithError(err).Error("Could not get document")
        Json.NewEncoder(w).Encode(&ErrorResponse{
            Error: "Could not finde merchant",})
        return
    }

    t := transactionModel{}
    if err := doc.DataTo(&t); err != nil {
        logrus.WithError(err).Error("Could not convert data")
    }
    logrus.Printf("payment succeeded: %v",t.Succeeded)

    if err := Json.NewEncoder(w).Encode(t); err != nil {
        logrus.WithError(err).Error("Could not encode Json")
    }
}

我在代码中找不到错误。你发现错误了吗?

我也认为这可能是某种缓存问题。所以我添加了 NoCache 中间件。

curl -X GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU -I
http/1.1 200 OK
Content-Type: text/plain; charset=utf-8
vary: Accept-EnCoding
Cache-Control: no-cache,no-store,no-transform,must-revalIDate,private,max-age=0
Expires: Thu,01 Jan 1970 00:00:00 UTC
Pragma: no-cache
vary: Origin
X-Cloud-Trace-Context: b521b80bb93676648d62054412ec30d0
Date: Wed,14 Jul 2021 23:44:15 GMT
Server: Google Frontend
Content-Length: 484

最后这里是一些日志,显示行为:

2021-07-14 23:31:44 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:44 default[20210715t012755]  time="2021-07-14T23:31:44Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:44 default[20210715t012755]  2021/07/14 23:31:44 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 94.649171ms
2021-07-14 23:31:45 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:45 default[20210715t012755]  time="2021-07-14T23:31:45Z" level=info msg="payment succeeded: true"
2021-07-14 23:31:45 default[20210715t012755]  2021/07/14 23:31:45 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 484B in 12.522468ms
2021-07-14 23:31:46 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:46 default[20210715t012755]  time="2021-07-14T23:31:46Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:46 default[20210715t012755]  2021/07/14 23:31:46 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 9.618494ms
2021-07-14 23:31:46 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:46 default[20210715t012755]  time="2021-07-14T23:31:46Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:46 default[20210715t012755]  2021/07/14 23:31:46 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 11.686507ms
2021-07-14 23:31:47 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:47 default[20210715t012755]  time="2021-07-14T23:31:47Z" level=info msg="payment succeeded: true"
2021-07-14 23:31:47 default[20210715t012755]  2021/07/14 23:31:47 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 484B in 35.701781ms
2021-07-14 23:31:48 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:48 default[20210715t012755]  time="2021-07-14T23:31:48Z" level=info msg="payment succeeded: true"
2021-07-14 23:31:48 default[20210715t012755]  2021/07/14 23:31:48 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 484B in 98.327564ms
2021-07-14 23:31:48 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:48 default[20210715t012755]  time="2021-07-14T23:31:48Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:49 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:50 default[20210715t012755]  time="2021-07-14T23:31:50Z" level=info msg="payment succeeded: true"
2021-07-14 23:31:50 default[20210715t012755]  2021/07/14 23:31:50 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 484B in 86.243205ms
2021-07-14 23:31:50 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:50 default[20210715t012755]  time="2021-07-14T23:31:50Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:50 default[20210715t012755]  2021/07/14 23:31:50 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 13.210213ms
2021-07-14 23:31:51 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:51 default[20210715t012755]  time="2021-07-14T23:31:51Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:51 default[20210715t012755]  2021/07/14 23:31:51 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 10.602363ms
2021-07-14 23:31:52 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:52 default[20210715t012755]  time="2021-07-14T23:31:52Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:52 default[20210715t012755]  2021/07/14 23:31:52 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 29.447106ms
2021-07-14 23:31:52 default[20210715t012755]  "GET /merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" 200
2021-07-14 23:31:52 default[20210715t012755]  time="2021-07-14T23:31:52Z" level=info msg="payment succeeded: false"
2021-07-14 23:31:52 default[20210715t012755]  2021/07/14 23:31:52 "GET http://.../merchant/lbk/U2qtsuSl3UXtaSOTNeWU http/1.1" from 127.0.0.1:62003 - 200 467B in 89.032489R_361_11845@s

我目前不知道该怎么办。我还检查了 Firestore 在状态仪表板上是否有活动事件。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的在 Go 中使用 AppEngine 从 Firestore 读取不一致全部内容,希望文章能够帮你解决在 Go 中使用 AppEngine 从 Firestore 读取不一致所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。