【问题标题】:Displaying values in descending order doesn't work按降序显示值不起作用
【发布时间】:2017-04-29 16:31:57
【问题描述】:

我正在使用 Ionic 和 Firebase 开发应用。

现在,我尝试按降序显示帖子:最新到最旧。问题是,无论我做什么来保存时间戳,它都会以某种方式总是将它从最旧的渲染到最新的。

我试过了:

  1. 按时间戳
  2. 按时间戳反转(-1 * 时间戳)
  3. 减去一个大数字 (9999999999999999 + (-1 * 时间戳)

总是相同的结果。

更新:这是我检索它的方法:

  var myId = firebase.auth().currentUser.uid;
    var ref = firebase.database().ref('/accounts/' + myId + '/discussions/');
    ref.orderByChild("timestamp").on("value", function(snapshot1) {
      this.data = [];
      snapshot1.forEach(function(child) {
      this.data.push(child.val());
    }.bind(this));

      $timeout(function(){
      $scope.discussions = this.data;

      })
    })

有什么想法吗?

【问题讨论】:

  • 可能是按字符串排序的。订购时日期时间类型很棘手。有没有办法指定类型是日期时间或时间戳?
  • 你好,试过了,结果完全一样。无论订单、字符串或日期格式如何。
  • 您需要像stackoverflow.com/questions/37883620/… 一样遍历快照的子节点(之前 christo8989 很有帮助地链接了该链接)。如果之后您仍然遇到同样的问题,请更新您问题中的代码。
  • 您好,我刚刚更新了我的原始帖子,向您展示了我如何使用这种方法显示它,我仍然有相同的结果...我不明白...
  • 我看不出你在哪里让它下降。也许我只是想念它?有没有我没看到的 .reverse() 地方?

标签: javascript firebase firebase-realtime-database


【解决方案1】:

使用 .reverse() 方法有效!

最终代码:

var myId = firebase.auth().currentUser.uid;
        var ref = firebase.database().ref('/accounts/' + myId + '/discussions/');
        ref.orderByChild("timestamp").on("value", function(snapshot1) {
          this.data = [];
          snapshot1.forEach(function(child) {
          this.data.push(child.val());
        }.bind(this));

          $timeout(function(){
          $scope.discussions = this.data.reverse() ;

          })
        })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 2014-05-06
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多