我有一個簡單的查詢函數,它在 for 循環中包含 Firestore 查詢,以查詢 js 文件中的所有文檔,每當我在終端上運行它時,輸出的第一行都是未定義的,然后顯示實際的預期輸出。旁注:我在 firestore 中只有三個文檔,每個文檔都有一個唯一的 id,即 id_1、id_2 和 id_3這是代碼: firebase.initializeApp(firebaseConfig); //firebaseConfig is a variable that hold the configuration details //Query Funtion function queryFirestore() { var database = firebase.firestore(); for (var i = 1; i <= 3; i++) { let id = database.collection("sampleCollection").doc(`id_${i}`); let getData = id.get().then(doc => { if (!doc.exists) { console.log("[!] Doc does not exists"); } else { var data = doc.data(); console.log(data['delayed']); //delayed is a boolean field in the Firestore Document } }).catch(err => { console.log("[:(] Error: ", err); }) } }queryFirestore();輸出:undefinedfalsefalsetrue
添加回答
舉報
0/150
提交
取消