Firestore Golang问题与原型类型

发布于 2025-02-14 01:57:16 字数 1646 浏览 0 评论 0原文

我试图理解为什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-02-21 01:57:16

在您的情况下,它可能使用的良好做法

err := b.DataTo(&u)
if err != nil {
   fmt.Println(err)
}

,在struct中有类型数据与Firestore中的模式不匹配。
如果我可以猜测StartDate字段。如果在Firestore模式中,则是timestamp字段,因此您必须在Golang结构中设置time

its good practice for using

err := b.DataTo(&u)
if err != nil {
   fmt.Println(err)
}

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 a timestamp field, so you must set time.Time in your golang struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文