简体   繁体   English

在Firebase查询中组合orderByChild和equalTo

[英]Combining orderByChild and equalTo in Firebase queries

Given the following data structure: 鉴于以下数据结构:

{
  "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/"
    }
  }
}

I would like to retrieve all the comments for a given link and sort them in reverse chronological order. 我想检索给定链接的所有注释,并按相反的时间顺序对它们进行排序。 I've gotten this far, but I can't figure out how to do the reverse sort because I've already used orderByChild to narrow down the results by link: 我已经做到了这一点,但我无法弄清楚如何进行反向排序,因为我已经使用orderByChild通过链接缩小结果范围:

ref.orderByChild('link').equalTo(currentLink).on('value', function (snap) {
  console.log(snapshot.val());
});

If I call orderByChild() a second time like this: 如果我第二次这样调用orderByChild():

ref.orderByChild('link').equalTo(currentLink).orderByChild('createdAt').on('value', function (snap) {
  console.log(snapshot.val());
});

it fails with this error message: 它失败并显示以下错误消息:

Uncaught Error: Query.orderByChild: You can't combine multiple orderBy calls. 未捕获的错误:Query.orderByChild:您无法组合多个orderBy调用。

I'm stumped. 我很难过。 Any suggestions? 有什么建议?

I encountered the same issues on my firebase project and I solved that like this. 我在firebase项目中遇到了同样的问题,我解决了这个问题。 Please take a look. 请看一下。

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

It will works. 它会奏效。 Best Regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase-具有equalTo的深度查询orderByChild - Firebase - Deep Query orderByChild with equalTo 带有 equalTo() 的 Firebase orderByChild 在 javascript 中不起作用 - Firebase orderByChild with equalTo() doesn't work in javascript firebase使用orderByChild()和equalTo()检索数据 - firebase retrieve data using orderByChild() and equalTo() Firebase orderByChild equalTo在没有数据时挂起 - Firebase orderByChild equalTo hangs when no data 无法使用orderByChild和equalTo从Firebase查询 - Unable to query from firebase using orderByChild and equalTo Firebase 实时数据库 - orderByChild().equalTo() 不返回预期节点 - Firebase Realtime Database - orderByChild().equalTo() does not return expected node 带有path和equalTo布尔值的Firebase orderByChild不返回任何内容 - Firebase orderByChild with path and equalTo boolean doesn't return anything Firebase实时数据库:4级嵌套上的orderByChild()。equalTo() - Firebase Realtime Database: orderByChild().equalTo() on 4th level nesting Firebase .orderByChild()。equalTo()。once()。then()当子元素不存在时的Promise - Firebase .orderByChild().equalTo().once().then() Promises when child doesn't exist 使用orderByChild和equalTo在Firebase查询上返回单个孩子的值 - Returning a single child's value on Firebase query using orderByChild and equalTo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM