无法使用查询从Cloud Firestore检索数据 [英] Unable to retrieve data from Cloud Firestore using queries

查看:100
本文介绍了无法使用查询从Cloud Firestore检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Cloud Firestore检索数据集合,以便可以将数据安排在 Bootstrap引导程序中。表格,显示了Firestore文档中的名称和分数。此处的FireStore布局。

I am attempting to retrieve a collection of data from my Cloud Firestore, so that I can arrange the data in a "Bootstrap" Table, displaying the name and the score from the Firestore documents.FireStore Layout Here.

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

I have created a reference to the user collection and queried this to obtain data, however when I run this it throws an exception "Uncaught ReferenceError: querySnapshot is not defined".

<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); //选择得分最高的10位玩家文件

My aim is to retrieve all of the documents inside the user collection, order and sort them using the inbuilt .limit and orderBy methods, then store them in an array so that they can be displayed using a "Bootstrap" table. var query = usersCollectionRef.orderBy("score").limit(10); //Selects the 10 highest scoring player's documents

推荐答案

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

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

You have to put the second part of your code within the then() function, as below. This is because get() returns "a promise that will be resolved with the results of the query." (see the Ref 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); 
                    });
            }

});






根据您的评论进行编辑:


EDIT following your comment:

如果给定名称的多个文档具有不同的分数(在数字字段分数中),则可以得到总得分如下:

In case you would have several documents for a given name which hold a different score (in a number field score), you could get the total score like that:

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);

});






编辑原始帖子后进行编辑


EDIT after edit of the original post

这样做

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 returns "An array of all the documents in the QuerySnapshot." (See Ref: https://firebase.google.com/docs/reference/js/firebase.firestore.QuerySnapshot#docs)
  • Then you use Array.from() to create the two arrays (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)

这篇关于无法使用查询从Cloud Firestore检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆