[英]Object doesn't support property or method 'forEach
我嘗試從 Firestore 上的 Firestore 獲取數據,但它返回此 object 沒有 foreach 方法。 我還嘗試在下面的控制台語句中進行控制台: console.log("this")
不起作用,但另一個 console.log 起作用。 這是代碼:
let fstore = firebase.firestore();
fStore
.collection('user-info')
.doc(userID)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
if (doc.id != userID) {
// doc.data() is never undefined for query doc snapshots
user_list2.push(doc.data());
console.log('this');
}
});
});
fStore
.collection('User')
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
if (doc.id != userID) {
// doc.data() is never undefined for query doc snapshots
user_list.push(doc.data());
console.log(user_list);
console.log(user_list.length);
}
});
});
fStore.collection("user-info").doc(userID).get()
不產生 QuerySnapshot。 它會生成一個 DocumentSnapshot,因為它只能返回一個文檔 - 您在doc()
中標識的文檔。 DocumentSnapshot 沒有 forEach 方法,因為沒有可迭代的內容。
你應該改為在 object 上調用 data() 你得到:
fStore
.collection('user-info')
.doc(userID)
.get()
.then(function (snapshot) {
const data = snapshot.data();
})
您的第二個查詢可以返回多個文檔,因為它要求獲取集合中的每個文檔,因此您會得到一個 QuerySnapshot intead。
這里的底線是 DocumentReference 上的get()
只能返回一個文檔,而 CollecitonReference 或 Query 上的get()
可以返回多個文檔。 它們是具有不同結果的不同類型的查詢。
let query = firestore.collection('col').where('foo', '==', 'bar');
query.get().then(querySnapshot => {
let docs = querySnapshot.docs;
for (let doc of docs) {
console.log(`Document found at path: ${doc.ref.path}`);
}
});
看這里: https://googleapis.dev/nodejs/firestore/latest/QuerySnapshot.html
您可以檢查文檔是否不為空
if (querySnapshot.docs.length === 0) {
return;
} else {
querySnapshot.forEach(doc => {
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.