我正在 Firestore 中迭代用戶文檔,但無法看到當前正在閱讀的文檔的 ID。? package main? import (? ? "context"? ? firebase "firebase.google.com/go"? ? "fmt"? ? "google.golang.org/api/iterator"? ? "google.golang.org/api/option"? ? "log"? )? ?ctx := context.Background()? ?sa := option.WithCredentialsFile("cert.json")? ?app, err := firebase.NewApp(ctx, nil, sa)? ?client, err := app.Firestore(ctx)? ?iter := client.Collection("users").Documents(ctx)? ?for {? ? ?doc, err := iter.Next()? ? ?if err == iterator.Done {? ? ? ?break? ? ?}? ? ?if err != nil {? ? ? ?fmt.Println("Error:", err)? ? ?}? ? userDoc := doc.Data()? ? var customer_id string? ? fmt.Println("user object ID: ", userDoc["mobile_number"])? ? // ^ Works? ? fmt.Println("user object ID: ", doc.documentID)? ? // ^ This gives me this error "? doc.documentID undefined?? ? // (type *"cloud.google.com/go/firestore".DocumentSnapshot? ? // has no field or method documentID)"據(jù)我從文檔中讀到,?DocumentSnapshot 應該有一個documentID返回快照文檔 ID 的屬性。我究竟做錯了什么?
如何獲取 Firestore 文檔的 ID?
幕布斯7119047
2023-07-26 17:25:33