【问题标题】:Unable to retrieve data from Cloud Firestore using queries无法使用查询从 Cloud Firestore 检索数据
【发布时间】:2018-10-13 17:06:24
【问题描述】:

我正在尝试从我的 Cloud Firestore 中检索数据集合,以便将数据排列在“引导”表中,显示 Firestore 文档中的名称和分数。FireStore Layout Here.

我创建了对用户集合的引用并查询它以获取数据,但是当我运行它时会抛出异常“未捕获的 ReferenceError:querySnapshot 未定义”。

<script>
var usersCollectionRef = db.collection("users"); //Creates a reference to the Users collection
var query = usersCollectionRef.orderBy("score", "desc").limit(10); //Creates a query based on the collection
query.get().then(function(querySnapshot) { //If query is needed
            if (querySnapshot.empty) { //Check whether there are any documents in the result
                console.log('no documents found');
            } else {
                    querySnapshot.docs.map(function (documentSnapshot) {
                        var name =      documentSnapshot.data().name;
                        var score =         documentSnapshot.data().score;
                        console.log(name + score);          
                    });
            }
});
</script>

我的目标是检索用户集合中的所有文档,使用内置的 .limit 和 orderBy 方法对它们进行排序和排序,然后将它们存储在一个数组中,以便可以使用“Bootstrap”表显示它们。 var query = usersCollectionRef.orderBy("score").limit(10); //Selects the 10 highest scoring player's documents

【问题讨论】:

    标签: javascript database firebase google-cloud-firestore


    【解决方案1】:

    给潜在读者的注意事项:答案的第一部分对应于 OP 的初始问题,在它被编辑之前。

    您必须将代码的第二部分放在then() 函数中,如下所示。 这是因为get() 返回“将通过查询结果解决的承诺”。 (参见参考文献https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference#get

    var usersCollectionRef = db.collection("users"); //Creates a reference to the Users collection
    
    var query = usersCollectionRef.where("name", "==", "Steeve"); //Creates a query based on the collection
    
    query.get().then(function(querySnapshot) { //Call get() to get a QuerySnapshot
    
                if (querySnapshot.empty) { //Check whether there are any documents in the result
                    console.log('no documents found');
                } else {
                        querySnapshot.docs.map(function (documentSnapshot) {
                            //Not necessary to do that  -> return documentSnapshot.data();
                            console.log(documentSnapshot.data().name); 
                        });
                }
    
    });
    

    根据您的评论进行编辑:

    如果给定名称的多个文档具有不同的分数(在数字字段 score 中),您可以获得这样的总分:

    var usersCollectionRef = db.collection("users"); //Creates a reference to the Users collection
    
    var query = usersCollectionRef.where("name", "==", "Steeve"); //Creates a query based on the collection
    
    query.get().then(function(querySnapshot) { //Call get() to get a QuerySnapshot
    
        var score = 0;
    
        if (querySnapshot.empty) { //Check whether there are any documents in the result
            console.log('no documents found');
        } else {
            var data = querySnapshot.docs.map(function (documentSnapshot) {
                //Not necessary to do that  -> return documentSnapshot.data();
                console.log(documentSnapshot.data().name);
                score += documentSnapshot.data().score;
            });
        }
    
        console.log(score);
    
    });
    

    编辑原帖后编辑

    这样做

    var query = usersCollectionRef.orderBy("score", "desc").limit(10); //Creates a query based on the collection
    query.get().then(function(querySnapshot) { //If query is needed
                if (querySnapshot.empty) { //Check whether there are any documents in the result
                    console.log('no documents found');
                } else {
                    var nameArray =  Array.from(querySnapshot.docs, x => x.data().name);
                    console.log(nameArray);
    
                    var scoreArray =  Array.from(querySnapshot.docs, x => x.data().score);
                    console.log(scoreArray);
                }
    });
    

    解释:

    【讨论】:

    • 好的,有道理!我添加了两个变量来记录文档中的名称和分数,但是有没有办法将它们迭代到数组中?我尝试使用具有 querySnapshot.docs 长度的 for 循环,但它似乎不起作用
    • 您不需要任何额外的循环:在querySnapshot.docs.map(function (documentSnapshot) {}) 中,您实际上是在循环查询返回的所有文档(即名称 = Steeve)
    • 对不起,我觉得我解释得不是很好,我已经更改了查询以检索所有文档并按分数排序。因此,此时控制台打印出得分最高的 10 个文档及其名称。我要做的是将 10 个最高分添加到 score[10] 数组中,将 10 个名称添加到 name[10] 数组中。
    • @LukeAlexanderThompson 你能用新代码编辑你原来的帖子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2021-05-05
    • 2021-06-19
    • 1970-01-01
    • 2023-03-09
    • 2021-08-15
    相关资源
    最近更新 更多