Cloud Firestore将登录用户与集合相关联

时间:2017-12-03 11:19:35

标签: firebase firebase-authentication google-cloud-firestore

我想知道为以下用例实现的最佳方法/模式。

我使用 Firebase身份验证注册用户,并且工作正常。
执行身份验证后,我需要从 Cloud Firestore 集合中提取,例如user_profile,与已登录用户相关的数据。

问题是该集合的id与用户无关,因为它是由Firestore自动生成的。因此,我不知道在<user>

中放入什么内容
db.collection("user_profile").doc(<user>).get()

我提出的一个可能的解决方案是将用户id存储在user_profile中,然后像这样查询整个集合

 db.collection("user_profile").where("id", "==", id_from_auth).get()

制作集合的id字段索引。

这是一个好方法吗?
是否有better/smarter/automatic方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

通常将每用户数据存储在由Firebase身份验证进行身份验证的用户的uid键入的文档中。