簡體   English   中英

forEach循環獲取數據火力地堡

[英]forEach loop for getting data firebase

我有一個代碼:

const { uid } = firebaseConfig.auth().currentUser;
const ref = db.collection('categories').doc(uid);

ref.onSnapshot(
  snapshot => {
    snapshot.forEach(item => console.log(item.data()));
  },
  err => {
    console.log(`Encountered error: ${err}`);
  }
);              

並且我得到了錯誤: Uncaught TypeError: snapshot.forEach is not a function我在哪里出錯了? Firebase版本: ^6.2.0

嘗試log快照並檢查其是否為陣列。 您可能正在尋找這個

由於您的ref指向特定的文檔, onSnapshot將使用一個沒有forEach()的單個DocumentSnapshot調用onSnapshot回調。

如果要附加到整個集合或查詢,則會得到一個QuerySnapshot ,它確實具有forEach方法。


如果您確實想為知道其ID的單個文檔獲取QuerySnapshot ,則可以對文檔ID使用查詢:

const ref = db.collection('categories').where(firebase.firestore.FieldPath.documentId(), '==', uid)

暫無
暫無

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

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