Reputation: 5254
I am using the code snippet from firestore repository:
I have a struct for UserFeed
type UserFeed struct {
date time.Time `firestore:"date,omitempty"`
reelUrl string `firestore:"reelUrl,omitempty"`
uid string `firestore:"uid,omitempty"`
username string `firestore:"username,omitempty"`
}
I am writing a getFeed function to get user feed like below:
func GetFeed(ctx context.Context, client *firestore.Client) error {
// [START fs_get_all_docs]
fmt.Println("All feed items:")
userID := "abcdefghsifkasfkhkfjlkdsaj"
userFeedRef := client.Collection("feed").Doc(userID).Collection("userFeed")
iter := userFeedRef.Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
var userFeed UserFeed
doc.DataTo(&userFeed)
// fmt.Println(userFeed)
fmt.Printf("Document data: %#v\n", userFeed)
fmt.Println(doc.Data())
}
// [END fs_get_all_docs]
return nil
}
Now when I run this, I get the output :
Document data: feed.UserFeed{date:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}, reelUrl:"", uid:"", username:""}
map[date:2020-08-15 07:06:16.183 +0000 UTC reelUrl:https:correctURL.com uid:correctUID username:somethingElse]
My firestore dosuments are stored as /feed/userName/userFeed/documents
I am not able to understand, why after conversion the data gets converted to 0s and nil.
Upvotes: 0
Views: 3881
Reputation: 21045
The returned data is not "converted to 0s and nil", these are the zero values for their corresponding data types (zero time object, empty strings).
For (un)marshalers to be able to function properly, they must be able to access the fields in your structs. This requires the fields to be exported (start with a capital letter).
Change your struct to the following (note: keep the name in the struct tag as it is in your schema, only change the field names):
type UserFeed struct {
Date time.Time `firestore:"date,omitempty"`
ReelUrl string `firestore:"reelUrl,omitempty"`
Uid string `firestore:"uid,omitempty"`
Username string `firestore:"username,omitempty"`
}
Upvotes: 3