我在firebase数据库上循环两个引用以获取一些数据并在页面上显示它们。我希望得到按其名称排序的结果,但它不起作用,并按照它们在数据库中的顺序不断返回元素。 这是我的代码:
var ref = firebase.database().ref('/point_of_interest/'+this.poi.chiave+'/tags/')
var ref1 = firebase.database().ref('/tag/');
let tagShow = [];
ref.once('value', function(snapshot){ //ciclo sui tag
snapshot.forEach(function(childSnapshot){
var childKey = childSnapshot.key; //chiave tag
var exists = false;
//ref1.orderByChild("nome").once('value', function(snapshot){
ref1.child("nome").orderByValue().once('value', function(snapshot){
snapshot.forEach(function(childSnapshot){
var childKey1 = childSnapshot.key;
if (childKey == childKey1){ //se ne trovo uno uguale, eisste nella lista dei tag del poi
exists = true;
}
return false;
})
}).then(a => {
if (!exists){
tagShow.push(childSnapshot); //push the list to be displayed
}
})
return false;
})
}).then(a=>{
//do something
})
我已尝试使用ref.orderByChild("nome")
和ref.child("nome").orderByValue()
,但效果相同。
这就是我的tag
引用的样子:
答案 0 :(得分:0)
Firebase数据库查询在您执行查询的位置下的每个子节点上运行。当您致电orderByValue()
时,它会对每个子节点的值进行排序。当你有这样的结构时,这很有用:
node: {
child1: "c"
child2: "b"
child3: "a"
}
此处使用ref.orderByValue()会按照其值定义节点:a
,b
,c
。
但在您的情况下,您要排序的值在JSON中低一级。在Firebase中说这是一个子节点,所以你在这里寻找orderByChild(...)
:
ref1.orderByChild("nome").once('value', function(snapshot){