如何排序和限制 firebase realtime db v9?
how to sort and limit firebase realtime db v9?
开发者。
我正在尝试制作一个 table 来显示得分最高的球员。
我是 firebase v9 的新手。
问题:
我正在使用实时数据库来存储文档名称和名称相同的用户的名称和分数。
因为我使用了 orderbyValue、orderbykey 和 orderbyChild。 snapshot.val() returns 一个包含对象(在底部检查)的对象是无序的,不知道如何访问全部或循环。
为了限制,我尝试了 limitToLast ,它工作正常但排序没有。
帮我整理和限制这个。
这就是我的数据在实时数据库中的存储方式
这是我要获取并尝试排序的代码
import { getDatabase, ref, set, onValue, query, orderByChild, limitToLast, equalTo, orderByValue, orderByKey } from "https://www.gstatic.com/firebasejs/9.4.1/firebase-database.js";
var namelist = $('.person');
var list = $('.scores');
// const scoreBoardRef = ref(db, 'users');
const scoreBoardRef = query(ref(db, 'users'), orderByChild('score'));
onValue(scoreBoardRef, (snapshot) => {
const data = snapshot.val();
console.log(data); // this returns object containing object and **i don't know how to deal with this.**
//console.log(scoreBoardRef);
list[0].textContent = data.score; // here i want score
namelist[0].textContent = "";//here i want name
//my main goal is to loop through top 3 or 10 data and print it to table
});
在控制台中,这是我得到的
⬇{abcd: {…}, man2: {…}, nacho: {…}, two: {…}}
▶abcd: {name: 'abcd', score: 15}
▶man2: {name: 'man2', score: 20}
▶nacho: {name: 'nacho', score: 21}
▶two: {name: 'two', score: 18}
▶[[Prototype]]: Object
正如 Dharmaraj 评论的那样:快照中的结果实际上是有序的,但是当您调用 snapshot.val()
时,它会转换为 JSON 对象并且 JSON 对象中的键是根据定义未排序。
要按顺序获得结果,请使用 snapshot.forEach
:
循环它们
onValue(scoreBoardRef, (snapshot) => {
snapshot.forEach((child) => {
const data = child.val();
console.log(data);
});
开发者。 我正在尝试制作一个 table 来显示得分最高的球员。 我是 firebase v9 的新手。
问题:
我正在使用实时数据库来存储文档名称和名称相同的用户的名称和分数。
因为我使用了 orderbyValue、orderbykey 和 orderbyChild。 snapshot.val() returns 一个包含对象(在底部检查)的对象是无序的,不知道如何访问全部或循环。
为了限制,我尝试了 limitToLast ,它工作正常但排序没有。
帮我整理和限制这个。
这就是我的数据在实时数据库中的存储方式
这是我要获取并尝试排序的代码
import { getDatabase, ref, set, onValue, query, orderByChild, limitToLast, equalTo, orderByValue, orderByKey } from "https://www.gstatic.com/firebasejs/9.4.1/firebase-database.js";
var namelist = $('.person');
var list = $('.scores');
// const scoreBoardRef = ref(db, 'users');
const scoreBoardRef = query(ref(db, 'users'), orderByChild('score'));
onValue(scoreBoardRef, (snapshot) => {
const data = snapshot.val();
console.log(data); // this returns object containing object and **i don't know how to deal with this.**
//console.log(scoreBoardRef);
list[0].textContent = data.score; // here i want score
namelist[0].textContent = "";//here i want name
//my main goal is to loop through top 3 or 10 data and print it to table
});
在控制台中,这是我得到的
⬇{abcd: {…}, man2: {…}, nacho: {…}, two: {…}}
▶abcd: {name: 'abcd', score: 15}
▶man2: {name: 'man2', score: 20}
▶nacho: {name: 'nacho', score: 21}
▶two: {name: 'two', score: 18}
▶[[Prototype]]: Object
正如 Dharmaraj 评论的那样:快照中的结果实际上是有序的,但是当您调用 snapshot.val()
时,它会转换为 JSON 对象并且 JSON 对象中的键是根据定义未排序。
要按顺序获得结果,请使用 snapshot.forEach
:
onValue(scoreBoardRef, (snapshot) => {
snapshot.forEach((child) => {
const data = child.val();
console.log(data);
});