Firestore 查询文档输出“未定义”,片刻后显示 Node.js 中 Firestore

我有一个简单的查询函数,它在 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();

输出:


undefined

false

false

true


蝴蝶不菲
浏览 40回答 1
1回答

一只萌萌小番薯

该错误是由于我在 console.log 中调用该函数导致未定义的内容作为输出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5