Firestore queried Document output 'undefined' and after few moments shows the correct data that is stored in Firestore in Node.js

Adithya Shetty

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
Adithya Shetty

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.

edited at
0

Comments

0 comments
Login to comment

Related

Firestore get path of already queried document

Firestore Document Typescript doc.data() undefined?

Is it possible for Firestore document Snapshot data to be undefined?

Multiple document reads in node js Firestore transaction

Firebase Firestore - Displaying Queried Data in React Native

Get first Firestore document from an order of documents (Node.js)

Fetching a firestore document from node.js errors

Flutter Firestore : Add a List of Data to Firestore Document

Unable to set state after getting document data from Firestore

Firestore get document after creation

Retrieve and Update data stored in Firestore

exporting firestore in node js

Splitting chunk of data in Firestore document

Filling Firestore document with data programmatically

How to push data to firestore document?

Can't get the correct document id in Firestore

Correct way to reference images in Firestore document

Firestore Document reference is always undefined in react native

getting a document from firestore, in a method that returns undefined

VUEJS + Firestore how to output Data?

Firebase firestore storing duplicate data -- Node.js

How to get timestamp format data from Firestore Node.js

How to produce data after getting firebase firestore in Vue.JS

How to add entirely new fields to queried Firestore (autoID) document from an Android app

Trying to runTransaction through all queried documents but getting FirebaseError: Provided document reference is from a different Firestore instance

Mongoose, NODE JS returns undefined but the correct data is collected within the findOne

Firestore not showing updated document after array update

Android Firestore get document ID after uploading

Firestore query after previous queries last document?