[英]Flutter - Firebase Auth UID registers as Null in Firestore
[英]Flutter - Firebase Firestore document uid does not exists
我的數據庫結構是這樣的:
域 -> 用戶 uid -> 用戶數據
如果我嘗試添加這樣的數據:
await FirebaseFirestore.instance
.collection(path)
.doc(firebaseUser.uid)
.collection(collectionName)
.add({...});
其中 path 是用戶域,數據庫顯示如下:
告訴我The document does not exists, it will not appear in queries or shapshots
。 但是,如果我像這樣通過自動 ID 添加相同的數據:
await FirebaseFirestore.instance
.collection(path)
.add({...});
它就像圖片中的第二個文檔一樣工作。 為什么會這樣?
查看此集合中的文檔如何在 Firestore 控制台中以斜體顯示:這意味着這些文檔僅作為一個或多個子集合的“容器”存在,但它們不是“真正的”文檔。
事實上,通過做
await FirebaseFirestore.instance
.collection(path)
.doc(firebaseUser.uid)
.collection(collectionName)
.add({...});
您在collectionName
(子)集合中創建文檔,但不在path
集合中。
另一方面,隨着
await FirebaseFirestore.instance
.collection(path)
.add({...});
您確實在path
集合中創建了一個文檔。
因此,如果您需要在path
集合和collectionName
(子)集合中有一個文檔,您需要創建這兩個文檔,而不僅僅是“子”文檔。
詳細說明:
下面以col1
集合下的一個doc1
文檔為例
col1/doc1/
和subCol1
(子)集合下的另一個subDoc1
col1/doc1/subCol1/subDoc1
實際上,從技術角度來看,它們之間根本沒有關系。 他們只是共享路徑的一部分,除此之外別無其他。
您可以很好地創建subDoc1
而無需創建doc1
。
這樣做的副作用是,如果您刪除文檔,其子集合仍然存在。 同樣,子集合文檔並沒有真正鏈接到父文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.