给定以下数据结构:

{
  "comments" : {
    "-JcBbk64Gpm1SKoFHv8b" : {
      "content" : "blah",
      "createdAt" : 1417550954985,
      "link" : "http%3A%2F%2Flocalhost%3A3000%2F",
      "recommendedCount" : 0,
      "replies" : {
        "-JcBbk8gF_nQ_vjwag61" : true
      },
      "replyCount" : 1
    },
    "-JcBbk8gF_nQ_vjwag61" : {
      "content" : "blah blah",
      "createdAt" : 1417550955151,
      "link" : "http%3A%2F%2Flocalhost%3A3000%2F",
      "recommendedCount" : 0,
      "replyCount" : 1,
      "replyToComment" : "-JcBbk64Gpm1SKoFHv8b"
    }
  },
  "links" : {
    "http%3A%2F%2Flocalhost%3A3000%2F" : {
      "author" : 5,
      "commentCount" : 2,
      "comments" : {
        "-JcBbk64Gpm1SKoFHv8b" : true,
        "-JcBbk8gF_nQ_vjwag61" : true
      },
      "createdAt" : 1417550954931,
      "ratingCount" : 2,
      "recommendedCount" : 32,
      "score" : 91,
      "title" : "A Christian vs. an Atheist: Round 2",
      "url" : "http://localhost:3000/"
    }
  }
}

我想检索给定链接的所有注释,并按相反的时间顺序对其进行排序。我已经走了很远,但是我无法弄清楚如何进行反向排序,因为我已经使用orderByChild通过链接缩小结果范围:
ref.orderByChild('link').equalTo(currentLink).on('value', function (snap) {
  console.log(snapshot.val());
});

如果我像这样第二次调用orderByChild():
ref.orderByChild('link').equalTo(currentLink).orderByChild('createdAt').on('value', function (snap) {
  console.log(snapshot.val());
});

它失败,并显示以下错误消息:



我很困惑有什么建议么?

最佳答案

我在firebase项目中遇到了相同的问题,我就这样解决了。
请看一下。

var formRef = firebase.child('links').child(currentLink);
formRef.orderByChild('createdAt').on('value', function (snap) {
  console.log(snapshot.val());
});

它将起作用。
此致。

10-08 18:29