Firestore Golang问题与原型类型
我试图理解为什么datato()
方法在传递protobuf中的struct后没有返回所有字段,但是如果我运行几乎相同的方法data()
我正在获取所有
type Employee struct {
StartDate string
Id string
Name string
Avatar string
}
list, err := client.Collection(Company).Doc(user.CompanyID).Collection(Workers).Documents(ctx).GetAll()
if err != nil {
log.Println(err.Error())
}
values := make([]*pb.Employee, len(list))
for i, v := range list {
log.Println("By Data", &pb.Employee{
StartDate: fmt.Sprint(b.Data()["startDate"]),
Name: b.Data()["name"].(string),
Avatar: b.Data()["avatar"].(string),
Id: b.Data()["id"].(string),
})
u := &pb.Employee{}
b.DataTo(u)
log.Println("by dataTo", u.StartDate, u.Name, u.Avatar, u.Id)
values[i]= u
}
log.Println(values)
输出:
By Data start_date:"2022-07-08 12:37:47.132904 +0000 UTC" id:"DILBuRmxVzVpOVG4iPuUeb8A4tN2" name:"alap" avatar:"https://image.com"
by dataTo
By Data start_date:"2022-07-08 12:37:39.901286 +0000 UTC" id:"bH6wuk0ooCMKsh7RQqlGWtXhIZr1" name:"Jack" avatar:"https://image3.com"
by dataTo Jack https://image3.com
[avatar:"https://image.com" name:"Jakub" avatar:"https://image3.com"]
因此,当您尝试通过Datato(P)
方法获取文档时,您可能会缺少上面的事情。有人可以看到我在这里做错了什么吗?
问候。
i am trying to understand why the DataTo()
method is not returning me all fields after passing the reference of struct in protobuf, but if i run almost same method Data()
i am getting all
type Employee struct {
StartDate string
Id string
Name string
Avatar string
}
list, err := client.Collection(Company).Doc(user.CompanyID).Collection(Workers).Documents(ctx).GetAll()
if err != nil {
log.Println(err.Error())
}
values := make([]*pb.Employee, len(list))
for i, v := range list {
log.Println("By Data", &pb.Employee{
StartDate: fmt.Sprint(b.Data()["startDate"]),
Name: b.Data()["name"].(string),
Avatar: b.Data()["avatar"].(string),
Id: b.Data()["id"].(string),
})
u := &pb.Employee{}
b.DataTo(u)
log.Println("by dataTo", u.StartDate, u.Name, u.Avatar, u.Id)
values[i]= u
}
log.Println(values)
Output:
By Data start_date:"2022-07-08 12:37:47.132904 +0000 UTC" id:"DILBuRmxVzVpOVG4iPuUeb8A4tN2" name:"alap" avatar:"https://image.com"
by dataTo
By Data start_date:"2022-07-08 12:37:39.901286 +0000 UTC" id:"bH6wuk0ooCMKsh7RQqlGWtXhIZr1" name:"Jack" avatar:"https://image3.com"
by dataTo Jack https://image3.com
[avatar:"https://image.com" name:"Jakub" avatar:"https://image3.com"]
so as u could see above things are missing when trying to get document via DataTo(p)
method. Anyone can see what i am doing wrong here ?
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,它可能使用的良好做法
,在
struct
中有类型数据与Firestore中的模式不匹配。如果我可以猜测
StartDate
字段。如果在Firestore模式中,则是timestamp
字段,因此您必须在Golang结构中设置time
。its good practice for using
maybe in your case, there is type data in the
struct
that didn't match with the schema in firestore.if I could guess the
StartDate
field. if in the firestore schema it is atimestamp
field, so you must settime.Time
in your golang struct.