I am trying to implement firebase with golang and have been successful so far. My code works fine when I run it locally and I can see the data in the firebase database. But when I deploy the code on GKE(GCP), it doesn't work and throws the following error. I am a beginner in goalng . Please help me fix this. What am I doing wrong? Please comment if you need more information
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0xd9ed72] goroutine 13154 [running]: firebase.google.com/go.(*App).Database(0x0, 0x12586c0, 0xc0000a6010, 0xc00153db98, 0x1, 0x1) /go/pkg/mod/firebase.google.com/go@v3.4.0+incompatible/firebase.go:85 +0x22 tracer.(*Storage).Publish(0xc0007140a0, 0x12586c0, 0xc0000a6010, 0xc0008442e0, 0x20, 0xc0016accf0, 0x10, 0xc0015a5ef0, 0xc0019c6c40, 0x3, ...) /project/pkg/tracer/storage.go:116 +0x642
tracer.(*Storage).Publish-fm(0x12586c0, 0xc0000a6010, 0xc0008442e0, 0x20, 0xc0016accf0, 0x10, 0xc0015a5ef0, 0xc0019c6c40, 0x3, 0x4, ...) /project/main.go:540 +0xd1 tracer.(*Factory).publishFunc.func1(0xc0007140c0, 0xc0008442e0, 0x20, 0xc0016accf0, 0x10, 0xc0015a5ef0, 0xc0019c6c40, 0x3, 0x4, 0xc0008442e0, ...) /project/pkg/tracer/bag.go:255 +0x102 created by tracer.(*Factory).publishFunc /project/pkg/tracer/bag.go:253 +0xf8
main.go
opt := option.WithCredentialsFile("./firebase.json")
conf := &firebase.Config{DatabaseURL: "https://xxx.xx.xx.com//"}
app, err := firebase.NewApp(context.Background(), conf, opt)
firego, err := app.Database(context.Background())
if err != nil{
log.Fatal(err)
}
ref := firego.NewRef("xxx-" + "dev")
usersRef := ref.Child(t)
k := make(map[string]string)
k["x_callID"] = content.ID
k["end_time"] = t2
k["user_id"] = content.UserID
err = usersRef.Set(context.Background(), k)
if err != nil {
log.Fatalln("Error setting value:", err)
}
Never ignore any error. Print error of this line. This might be the root cause.
app, err := firebase.NewApp(context.Background(), conf, opt)
If firebase.NewApp()
fail, it will return error and app
will be nil. As you are trying to access app.Database()
on next line, you will get that panic since app
is nil.