【问题标题】:JavaScript - how to order firebase data by key of childsJavaScript - 如何通过孩子的键订购 Firebase 数据
【发布时间】:2018-04-09 20:55:40
【问题描述】:

我的 Firebase 数据采用以下结构:

我正在像这样查询我的数据:

var db_ref = firebase.database().ref('/playlist-data/');
db_ref.once('value', function(snapshot){
   snapshot.forEach(function(childSnapshot) {
     var artist = childSnapshot.child('artist').val();
     var position = childSnapshot.child('position').val();
     // do something with artist & position
  });
});

但是我想通过'playlist-data'的每个子节点的键'position'对返回的快照数据进行排序,这样我就可以按升序排列数据。我怎样才能实现类似的目标?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    要通过position 订购,请尝试以下操作:

    db_ref.orderByChild("position").once('value', function(snapshot){
    

    来自文档: https://firebase.google.com/docs/reference/js/firebase.database.Reference#orderByChild

    orderByChild

    生成一个按指定子键排序的新 Query 对象。

    查询一次只能按一个键排序。在同一个查询中多次调用orderByChild() 是错误的。

    【讨论】:

    • 在这种情况下,我在 '/playlist-data'/ 中有 'n' 个节点,我正在尝试查询每个节点,所以我不知道或有 equalTo 值跨度>
    • 好的,我明白你的意思了,db_ref.orderByChild('position').once('value', function(snapshot){
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2017-07-30
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多