簡體   English   中英

文檔引用在集合引用上必須有偶數段錯誤

[英]Document references must have an even number of segments error on a collection reference

我收到了一個相當簡單的錯誤:

Document references must have an even number of segments.

我知道它告訴我什么以及如何修復它,但是它在集合引用上給了我這個錯誤。

CollectionReference collectionReference = getFirebaseInstance()
.collection(Constants.USERS)
.document(userId)
.collection(Constants.CONTACTS);

在我的數據庫中,路徑是 (COLLECTION) < DOCUMENTS > (COLLECTION) < DOCUMENTS > 我試圖在 users/{id}/contacts 獲取用戶的所有聯系人,但它只是拋出這個錯誤,有什么想法嗎?

getFirebaseInstance 是我創建的一種方法,用於始終獲取當前

FirebaseFirestore.getInstance();

您的userId變量可能包含斜杠。 文檔ID不能有斜杠,因為它們在形成文檔的“路徑”時被視為集合和文檔之間的分隔符。

字符串也可能是空的,這是無效的。

因為上述和其他類似的帖子都沒有幫助我..

我在調用set數據時遇到此錯誤,然后在.getDocument(...)之后立即(即讀取數據)。 當我在setData方法的完成處理程序中調用我的讀取數據方法時,錯誤消息消失了。

我發現了同樣的問題,這就是解決我的問題的方法:

我嘗試使用以下方法設置文檔:

setDoc()

不提供文檔 ID。

所以我用:

addDoc()

所以 firebase 本身為文檔提供了一個 ID。

lecon 是:使用 set 你必須提供一個 ID,使用 add 你沒有提供 ID firebase 為你做。

謝謝 !

我不確定你的情況,但是,如果你有這樣的 function,它會從你的 firebase 中通過“Id”帶來數據,只需在集合名稱后添加一個斜杠“/”

getDetailProduct(id: string): Observable<Interface_name> {
const productsDocuments = this.angularFirestore.doc<Interface_name>(
  'collection_name/' + id  //here after your collection name add "/"
);
return productsDocuments.snapshotChanges().pipe(
  map((changes) => {
    const data = changes.payload.data() as Interface_name;
    const id = changes.payload.id;
    return { id, ...data };
  })
);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM