I have a simple query function which has the Firestore query inside a for loop to query all the documents inside my js file and whenever I run it on the terminal the first line of the output is undefined and then the actual expected output gets displayed.
sidenote: I have only three documents in firestore and each with a unique id as id_1, id_2 and id_3
here's the code:
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();
Output:
undefined
false
false
true
The error was due to I was calling the function inside a console.log which was causing the undefined to come as the output.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments