如何在使用Golang的Firestore中插入引用类型字段

huangapple go评论91阅读模式
英文:

How to insert a reference type field on Firestore with Golang

问题

在我的数据库中,我在一些文档中使用了引用类型的字段,就像这样:

如何在使用Golang的Firestore中插入引用类型字段

我试图将这些属性插入到Firestore中:

	_, err = userReference.Set(context.Background(), models.User{
		Jobs:               []*firestore.DocumentRef{employeeReference},
	})

在这种情况下,我使用了一个*DocumentRef的数组,但即使是一个唯一的DocumentRef也不起作用,我还尝试将其插入为DocumentRef类型而不是指针,也不起作用,我的User类型是这样的:

type User struct {
	Jobs               []*firestore.DocumentRef `json:"jobs"`
}

有什么办法可以从Go中插入这种类型的数据吗?在JavaScript SDK中,我记得只能直接将DocumentReference类型插入到已实现的对象中,但我在使用Golang时遇到了这个问题。

英文:

Inside my database i'm using fields of reference type on some documents, like this:

如何在使用Golang的Firestore中插入引用类型字段

I'm trying to insert these property on firestore

	_, err = userReference.Set(context.Background(), models.User{
		Jobs:               []*firestore.DocumentRef{employeeReference},
	})

On this case I used an array of *DocumentRef, but even if is a unique DocumentRef doesn't work, I also tried to insert as type DocumentRef instead of the pointer, and also doesn't work, my User type is like that:

type User struct {
	Jobs               []*firestore.DocumentRef `json:"jobs"`
}

There's something that I can do to insert this type of data from go? On Javascript SDK, I remember that is only to insert the DocumentReference type directly on object that is achieved, but I'm facing this problem with Golang.

答案1

得分: 1

我对Golang没有经验,但是你可以在这里找到许多示例。

我想知道下面的代码是否能给你一些提示:

_, err := userReference.Set(context.Background(), models.User{
        Jobs:               []*firestore.DocumentRef{client.Doc("/selfManagedEmployees/K4qhd5k1c...")}	
})
英文:

I have no experience with Golang but here you can find many examples.

I wonder if something like the following can give you any hints:

_, err := userReference.Set(context.Background(), models.User{
        Jobs:               []*firestore.DocumentRef{client.Doc("/selfManagedEmployees/K4qhd5k1c...")}	
})

huangapple
  • 本文由 发表于 2021年11月1日 20:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/69797221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定