gpt4 book ai didi

javascript - 获取快照元素的数量

转载 作者:行者123 更新时间:2023-12-03 00:50:27 25 4
gpt4 key购买 nike

我试图根据一个值对我的用户进行排名,为此,我尝试在按名为“earned_points”的值排序后迭代列表,但是我收到错误消息 dataSnapshot .nu​​mChildren 不是函数

这是我的代码的样子

return rankref.orderBy("earned_points").limit(10).get().then(function(dataSnapshot) {
let i = 0;
console.log(dataSnapshot)
dataSnapshot.forEach(function(childSnapshot) {
const r = dataSnapshot.numChildren() - i;
console.log(childSnapshot)
updates.push(childSnapshot.ref.update({rank: r}));
leaderboard[childSnapshot.key] = Object.assign(childSnapshot.val(), {rank: r});
i++;
});

updates.push(leaderboardRef.set(leaderboard));
return Promise.all(updates);

这应该为每个子快照添加排名,然后创建一个名为排行榜的新节点。

知道为什么我会收到这个吗?我刚刚从实时数据库切换到 firestore,不知道发生了什么

最佳答案

i just switched from realtime database to firestore and don't know what's going on

在firestore中,没有DataSnapshot,firestore使用集合和文档的概念。

方法 numChildren() 位于类 DataSnapshot 内部.

get()方法位于类 CollectionReference 内部,它返回一个 QuerySnapshot,因此您会收到错误 dataSnapshot.numChildren 不是函数

检索集合中所有文档的示例:

db.collection("cities").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
});

检查一下:

https://firebase.google.com/docs/firestore/query-data/get-data

关于javascript - 获取快照元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53069689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com