【问题标题】:Panic: runtime error: invalid memory address or nil pointer dereference only on the GAE恐慌:运行时错误:仅在 GAE 上无效的内存地址或 nil 指针取消引用
【发布时间】:2019-09-12 11:52:45
【问题描述】:

我正在使用 gin 框架开发 golang 应用程序。基本上它只是从 Firestore 中获取 JSON 格式的数据。

Localy 它运行良好,但是当我将它部署到 GAE(gcloud app deploy)时,部署期间没有错误,但是当访问页面时它不起作用并且在日志中提供错误:“恐慌:运行时错误:无效内存地址或 nil 指针取消引用"

包列表集合

import (
    "fmt"
    "log"
    "net/http"

    "cloud.google.com/go/firestore"
    "github.com/gin-gonic/gin"
    "google.golang.org/api/iterator"
    "google.golang.org/appengine"
)

func main() {

}

//GetListCollections function

func GetListCollections(c *gin.Context) {

    var coll []string
    ctx := appengine.NewContext(c.Request)

    projectID := "XXX"
    client, err := firestore.NewClient(ctx, projectID)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }
    defer client.Close()

    iter := client.Collection("collection").Documents(ctx)

    for {

        doc, err := iter.Next()

        if err == iterator.Done {
            break
        }
        if err != nil {
            fmt.Println("ERROR")
        }

        coll = append(coll, doc.Data()["Title"].(string))

    }

    c.JSON(http.StatusOK, gin.H{
        "collections": coll,
    })

}

【问题讨论】:

  • 您应该至少 指出您的代码在哪一行失败。
  • 您没有处理 iter.Next 返回的错误。您只需打印它,然后像什么都没发生一样继续进行。

标签: go google-app-engine go-gin


【解决方案1】:

因为没有人知道它发生在哪里?

通过分析您的代码,我能想到的唯一可能性是您的 itr 变量为空。

您可能需要更改检查错误部分并添加 Panic 而不是仅打印错误并继续运行

        if err != nil {
            panic("ERROR")
        }

【讨论】:

  • 问题出在 if err != nil { panic("ERROR") } 但它在本地工作,而不是在部署到 GAE 时
  • 现在我在 GAE 中有一个错误:恐慌:rpc 错误:代码 = PermissionDenied desc = 权限缺失或不足
猜你喜欢
  • 2015-02-24
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2013-04-23
  • 2019-03-09
  • 2018-09-27
相关资源
最近更新 更多