我正在使用 NextJS 和 Typescript 進行 Firebase 專案。我正在嘗試從 Firestore 資料庫中獲取資料,但出現此錯誤Type 'Promise<QuerySnapshot<DocumentData>>' must have a '[Symbol.iterator]()' method that returns an iterator.
這是我用來從資料庫中獲取資料的代碼
const postsRef = collection(db, "discussions");
query(postsRef, orderBy("createdAt"), limit(20));
const [posts] = getDocs(postsRef)
uj5u.com熱心網友回復:
您getDocs沒有回傳一個陣列,這就是您收到此錯誤的原因。AQuerySnapshot是具有forEach(類似于陣列)但不是陣列的自定義 Firebase 型別。因此,您需要使用以下命令遍歷它forEach:
const postsRef = collection(db, "discussions");
query(postsRef, orderBy("createdAt"), limit(20));
// I'm guessing you need await here too?
const postDocs = await getDocs(postsRef)
let posts = []
postDocs.forEach((postDoc) => {
posts = [...posts, {
...postDoc.data(),
id: postDoc.id,
}]
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/336913.html
標籤:反应 火力基地 谷歌云firestore 下一个.js
