簡體   English   中英

我無法訪問 firestore 數據庫中的兩個子集合之一。 反應

[英]I can't access one of two subcollections in firestore data base. React

我在同一個文檔中有兩個子集合你可以在圖片中看到

我可以使用此代碼訪問第一個:

useEffect(() => {
const getData = () => {                                       // get data from firestore to app
 
  const q = query(collection(db, `lists/my lists/${currentList}`), orderBy('queryPreference'));

  onSnapshot(q, (snapshot) => {
    firestoreList = [];                                       // reset firestoreList after every change to avoid copies in items array
    firestoreIds = [];
    snapshot.docs.forEach((doc) => {
      console.log('subcol item', doc)
      firestoreList.push({ ...doc.data(), id: doc.id });
      firestoreIds.every(id => id !== doc.id) && firestoreIds.push(doc.id);
      
    });
    if (firestoreList.length === 0) {
      setItems(items.concat(newItem));
    } else {
      setItemIds(firestoreIds);
      setItems(firestoreList);
    }
  }); 
}

getData();
console.log('get data test')
}, [currentList]);

其中currentList被硬編碼為“購物清單”。 如果我將currentList更改為“待辦事項列表”,firebase 不會返回任何內容(也沒有錯誤)。 collections 的名稱和 ID 拼寫如上:'shopping list' 和 'to do list'。

它應該是這樣的:

const q = query(collection(db, 'lists', 'my lists', currentList), 
orderBy('queryPreference'));

暫無
暫無

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

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