how to order by timestamp using Firebase?

后端 未结 1 703
半阙折子戏
半阙折子戏 2021-01-05 10:56

I\'m trying to order by timestamp in Firebase using orderByChild() but it\'s not working. The results seem to be ordered by the key and Firebase seems to be ign

相关标签:
1条回答
  • 2021-01-05 11:42

    Works for me:

    firebase.database().ref(endpoint)
      .orderByChild('timestamp')
      .on('value', function(snapshot) {
      this.data = [];
    
      snapshot.forEach(function(child) {
        this.data.push(child.val());
      }.bind(this));
    
      console.log("all", data.map(function(val) { return new Date(val.timestamp).toString(); }));
    });
    

    "all"

    ["Fri Jun 17 2016 06:51:33 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:43 GMT-0700 (PDT)"] Blockquote

    firebase.database().ref(endpoint)
      .orderByChild('timestamp')
      .startAt(1466171497190)
      .endAt(1466171498405)
      .on('value', function(snapshot) {
      this.data = [];
    
      snapshot.forEach(function(child) {
        this.data.push(child.val());
      }.bind(this));
    
      console.log("filtered", data.map(function(val) { return new Date(val.timestamp).toString(); }));
    
    });
    

    "filtered"

    ["Fri Jun 17 2016 06:51:37 GMT-0700 (PDT)", "Fri Jun 17 2016 06:51:38 GMT-0700 (PDT)"]

    http://jsbin.com/humatuquju/edit?js,console

    0 讨论(0)
提交回复
热议问题