javascript - Firebase orderByChild 没有重复数据
问题描述
我正在尝试对 Firebase 数据进行排序和显示,但是我被卡住了。我的数据库如下所示:
我需要对数据进行排序nr_of_matches
,我可以通过这样的查询来做到这一点:
const ref = firebase.database().ref('Matches/'+db_name).orderByChild('nr_of_matches')
ref.once('value', function (snapshot) {
snapshot.forEach(function (childSnapshot) {
const matches = childSnapshot.val().matches
const nr_of_matches = childSnapshot.val().nr_of_matches
const the_matched = childSnapshot.val().the_matched
console.log("matches : "+matches)
});
});
但是,我像这样检索重复的数据。为了不得到重复的数据,我可以这样做:
const ref = firebase.database().ref('Matches/'+db_name+"/"+child.key).orderByChild('nr_of_matches')
ref.on('value', function (snapshot) {
const matches = snapshot.val().matches
const nr_of_matches = snapshot.val().nr_of_matches
const the_matched = snapshot.val().the_matched
console.log("matches : "+matches)
});
但后来它不再排序了。知道如何解决这个问题吗?
解决方案
如果您不想检索重复的数据,那么您可以使用query
:
const ref = firebase.database().ref('Matches/'+db_name+"/").orderByChild('nr_of_matches').equalto(2);
ref.on('value', function (snapshot) {
const matches = snapshot.val().matches
const nr_of_matches = snapshot.val().nr_of_matches
const the_matched = snapshot.val().the_matched
console.log("matches : "+matches)
});
推荐阅读
- python - 试图将 bmp 图像添加到 ScrolledText 小部件,它说它不存在(Tkinter)
- nodes - 私有 BSC 节点每秒来自远程客户端的请求数太少
- jquery - 由 Sortable JS 克隆且其中包含 Select2 的项目不起作用
- react-native - 在 MapView 标记周围添加阴影
- python - 'ascii' 编解码器在使用 python pandas 和 xlsxwriter 将表从 OracleDB 导出到 excel 时无法解码字节 0xc2 序数不在范围内(128)
- arrays - Google Apps 脚本 - 随机化下拉列表 - 选择索引
- java - 运行 docker build -t 时出现 Docker 问题
- javascript - 复杂解构:数组的第一个索引内的对象在另一个对象内
- python - 调试模式下的 Flask Restful 自定义错误处理程序
- html - 为什么我的下拉菜单不起作用?(HTML&CSS)