无法使用查询从Cloud Firestore检索数据

时间:2018-05-03 11:35:30

标签: javascript database firebase google-cloud-firestore

我正在尝试从我的Cloud Firestore中检索数据集合,以便我可以将数据安排在" Bootstrap"表,显示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方法对它们进行排序和排序,然后将它们存储在一个数组中,以便使用&#34; Bootstrap&#显示它们34;表。 var query = usersCollectionRef.orderBy("score").limit(10); //Selects the 10 highest scoring player's documents

1 个答案:

答案 0 :(得分:1)

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

您必须将代码的第二部分放在then()函数中,如下所示。 这是因为get()返回&#34;一个将使用查询结果解决的承诺。&#34; (参见参考文献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);
            }
});

说明: