【问题标题】:get data from firebase orderByChild doesn't order the items从 firebase orderByChild 获取数据不订购商品
【发布时间】:2023-03-08 13:04:02
【问题描述】:

我正在遍历 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 参考的样子:

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database firebase-storage


    【解决方案1】:

    Firebase 数据库查询在您执行查询的位置下的每个子节点上运行。当您调用orderByValue() 时,它会对每个子节点的值进行排序。当您有这样的结构时,这很有用:

    node: {
      child1: "c"
      child2: "b"
      child3: "a"
    }
    

    这里使用 ref.orderByValue() 将根据节点的值对节点进行排序:abc

    但在您的情况下,您想要排序的值在 JSON 中低一级。在 Firebase 中,这是一个子节点,因此您在此处寻找 orderByChild(...)

    ref1.orderByChild("nome").once('value', function(snapshot){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      相关资源
      最近更新 更多