Reputation: 5115
I want to fetch a document from Firestore and have it marshalled into my custom struct. Currently all data is unmarshalled, but I can't find a way to get the document ID into my struct. Here is an example:
type MyStruct struct {
ID string // What to put here?
PropA string `firestore:"prop_a"`
PropB string `firestore:"prop_b"`
}
doc, err := client.Doc(docref).Get(ctx) // Fetch document
var x MyStruct // Allocate object
err = doc.DataTo(&x) // Unmarshal
The result is that PropA
and PropB
are populated on x
, but I have no idea how to populate the ID field with the document ID.
I can obviously just manually fish it out of doc
, but it feels like DataTo
should be able to handle this.
Upvotes: 1
Views: 392
Reputation: 11
The id is not part of the document data. Assign it as you mentioned:
x.ID = doc.Ref.ID
Upvotes: 1