Javascript 无法使用查询从云Firestore检索数据
我正试图从我的云Firestore中检索一组数据,以便将数据安排在“引导”表中,显示Firestore文档中的名称和分数 我已经创建了一个对用户集合的引用,并对此进行查询以获取数据,但是当我运行此操作时,它会抛出一个异常“UncaughtReferenceError:querySnapshot未定义”Javascript 无法使用查询从云Firestore检索数据,javascript,database,firebase,google-cloud-firestore,Javascript,Database,Firebase,Google Cloud Firestore,我正试图从我的云Firestore中检索一组数据,以便将数据安排在“引导”表中,显示Firestore文档中的名称和分数 我已经创建了一个对用户集合的引用,并对此进行查询以获取数据,但是当我运行此操作时,它会抛出一个异常“UncaughtReferenceError:querySnapshot未定义” var userscolectionref=db.collection(“用户”)//创建对用户集合的引用 var query=usersCollectionRef.orderBy(“分数”、“
var userscolectionref=db.collection(“用户”)//创建对用户集合的引用
var query=usersCollectionRef.orderBy(“分数”、“描述”)。限制(10)//基于集合创建查询
query.get().then(函数(querySnapshot){//如果需要查询
如果(querySnapshot.empty){//请检查结果中是否有任何文档
console.log(“未找到文档”);
}否则{
querySnapshot.docs.map(函数(documentSnapshot){
var name=documentSnapshot.data().name;
var score=documentSnapshot.data().score;
console.log(名称+分数);
});
}
});
我的目标是检索用户集合中的所有文档,使用内置的.limit和orderBy方法对它们进行排序和排序,然后将它们存储在一个数组中,以便使用“Bootstrap”表显示它们。
var query=usersCollectionRef.orderBy(“分数”).limit(10)//为潜在读者选择得分最高的10个玩家文档
注意:答案的第一部分对应于OP编辑前的初始问题
您必须将代码的第二部分放入then()
函数中,如下所示。
这是因为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
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);
}
});
说明:
- docs返回“querySnapshot中所有文档的数组”(请参阅参考:)
- 然后使用Array.from()创建这两个数组(请参见)
好吧,这很有道理!为了记录文档中的名称和分数,我添加了两个变量,但是有没有方法将它们迭代到数组中?我尝试使用一个长度为querySnapshot.docs的for循环,但它似乎不起作用您不需要任何额外的循环:在querySnapshot.docs.map(function(documentSnapshot){}
中,您实际上是在查询返回的所有文档上循环(例如,对于其中的name=Steeve)对不起,我认为我解释得不太好,此后,我将查询更改为检索所有文档并按分数排序。因此,此时控制台会打印出得分最高的10个文档及其名称。我想做的是将10个最高分添加到分数[10]数组,将10个名字添加到名称[10]数组。@LukeAlexanderThompson你能用新代码编辑你的原始帖子吗?
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);
}
});