我正在使用 gin 框架开发 golang 应用程序。基本上它只是以 JSON 格式从 firestore 获取数据。
在本地它工作得很好,但是当我将它部署到 GAE (gcloud app deploy) 时,部署期间没有错误,但是当访问页面时它不起作用,并且在日志中提供了一个错误:“panic: runtime error: invalid内存地址或 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,
})
}
最佳答案
因为没有人知道它发生在哪里?
从分析你的代码,我能想到的唯一可能性是你的
itr
变量为空。
您可能需要更改错误检查部分并添加 Panic 而不是仅打印错误并继续运行
if err != nil {
panic("ERROR")
}
关于go - panic : runtime error: invalid memory address or nil pointer dereference only on the GAE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55808776/