【发布时间】: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