Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript-如何按childs键排序firebase数据_Javascript_Firebase_Firebase Realtime Database - Fatal编程技术网

JavaScript-如何按childs键排序firebase数据

JavaScript-如何按childs键排序firebase数据,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我的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 positi

我的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
  });
});
但我想按“播放列表数据”的每个子节点的键“位置”对返回的快照数据进行排序,以便按升序排列数据。如何实现类似的目标?

要按职位排序,请尝试以下操作:

db_ref.orderByChild("position").once('value', function(snapshot){
从文档中:

orderByChild

生成按指定子键排序的新查询对象

查询一次只能按一个键排序。对同一查询多次调用orderByChild是一个错误


在这种情况下,我在“/playlist data”中有“n”个节点,我试图查询每个节点,所以我不知道或没有相等的值OK,我现在明白你的意思了,db\u ref.orderByChild'position'。一旦“value”,functionsnapshot{