我正在尝试初始化 firebase 客户端并连接到 firebase 数据库。
package main
import (
"log"
"cloud.google.com/go/firestore"
"google.golang.org/api/option"
)
var (
firestoreClient *firestore.Client
)
func initializeFirestore() {
var err error
credentialsFilePath := "config/serviceaccount.json"
opt := option.WithCredentialsFile(credentialsFilePath)
firestoreClient, err = firestore.NewClient(ctx, "test", opt)
if err != nil {
log.Fatalf("Failed to create Firestore client: %v", err)
}
_, err = firestoreClient.Collections(ctx).Next()
if err != nil {
log.Fatalf("Failed to verify Firestore connection: %v", err)
}
}
我不断收到错误
无法验证 Firestore 连接:rpc 错误:code = NotFound desc = 用于项目测试的数据库(默认)不存在 请访问 https://console.cloud.google.com/datastore/setup?project=cyberprayer添加 Cloud Datastore 或 Cloud Firestore 数据库。
退出状态 1
我创建了名为 test 的数据库,它是本机模式。我不知道为什么它一直连接到我没有的(默认)。
如创建客户端的文档所示:
ctx := context.Background()
client, err := firestore.NewClient(ctx, "projectID")
所以
NewClient
的第二个参数是项目ID。由于您在那里传递了 test
,因此它会查找 ID 为 test
的项目,但该项目不存在。
解决此问题的第一步是在其中传递实际的项目 ID。
当您这样做时,我将看看如何(或者是否可能)在 Go SDK 中传递数据库名称。