我试图根据一个值对我的用户进行排名,为此,我试图在按一个名为"earned_points"的值排序后迭代列表,但我收到了错误消息dataSnapshot.numChildren is not a function
。
以下是我的代码看起来像
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,不知道发生了什么
我刚从实时数据库切换到firestore,不知道发生了什么
在firestore中,没有DataSnapshot
,firestore使用集合和文档的概念。
方法numChildren()
在类DataSnapshot
内部。
get()
方法在类CollectionReference
中,它返回一个QuerySnapshot
,因此您得到一个错误dataSnapshot.numChildren is not a function
检索集合中所有文档的示例:
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