简体   繁体   English

Firebase(Web)无法使orderByChild正常工作

[英]Firebase (Web) can't get orderByChild to work

I know that there are 1,000 answers out there to this question, which is partially why I'm so confused by the fact that I can't get it to work. 我知道这个问题有1,000个答案,部分原因就是为什么我对我无法正常工作感到困惑。

I can't get the orderByChild query / filter to work. 我无法使orderByChild查询/过滤器正常工作。 I've looked at at least 25 tutorials, guides, and articles online that explain it and they all seem to say to do exactly what I'm doing: 我看了至少25篇在线讲解的教程,指南和文章,它们似乎都说完全符合我的意思:

var dbRef = firebase.database().ref();
var filterExperience = dbRef.child("Users").orderByChild('trade_general').equalTo('Construction');

filterExperience.on('child_added', snap => {

  // Just trying to figure out if anything's even happening
  console.log("Test" + snap.val());    

  // Set Card ID    
  $('#cand-card').attr('id', 'cand-card' + snap.key);

  // Set Name
  $('#fullname').attr('id', 'fullname' + snap.key);
            $('#fullname' + snap.key).text(snap.child("Personal").child("first_name").val() + " " + snap.child("Personal").child("last_name").val());

// And so on...

And, for the record, firebase is all set up properly. 并且,记录下来,firebase均已正确设置。 I have the snippet at the bottom of my html page and I've been reading / writing records with no issue. 我的html页面底部有代码段,而且我一直在读/写记录,没有任何问题。

My database looks like this: 我的数据库如下所示:

Users
|
|--KYQ0L5TStcZM1rgVpte
    |
    |-Account
    |-Availability
    |-Education
    |-Location
    |-Work_History
      |
      |-Recent_Job_1
        |
        |-employer_name: "abc company"
        |-job_position: "concrete finisher"
        |-trade_general: "Construction"

The result that I'm getting is: absolutely nothing. 我得到的结果是:绝对没有。 Nothing in the console from that test that I run right after the 'snap'. 在该测试之后,控制台中没有任何东西可以在“快照”之后立即运行。 Nothing on the page. 页面上没有任何内容。

Firebase Database queries consider each child under the location you query and then take the child+conditions that you specify. Firebase数据库查询会在您查询的位置下考虑每个子项 ,然后采用您指定的子项+条件。 So if the path is fixed you can do: 因此,如果路径固定,则可以执行以下操作:

dbRef.child("Users")
     .orderByChild('Work_History/Recent_Job_1/trade_general')
     .equalTo('Construction')

So this consider the Work_History/Recent_Job_1/trade_general for each user and filters based on that. 因此,请考虑每个用户的Work_History/Recent_Job_1/trade_general ,并根据该条件进行过滤。

But most likely Recent_Job_1 is a dynamic path. 但是最有可能的“ Recent_Job_1是动态路径。 In that case you're trying to query for a property at a nested, dynamic path, which is not possible in the query model of the Firebase Database. 在这种情况下,您将尝试在嵌套的动态路径中查询属性,这在Firebase数据库的查询模型中是不可能的。 Firebase's indexes are essentially single-value maps and this type of query wouldn't work with those. Firebase的索引本质上是单值映射,这种查询不适用于这些值。

You'll have to find a different data structure to allow the query you want. 您将必须找到其他数据结构以允许所需的查询。

One way to do that would be to hold the latest trade_general value as a direct property under each user. 一种方法是将最新的trade_general值保留为每个用户的直接属性。 That would allow you to filter by the latest job's trade_general value. 这样一来,您就可以按最新职位的trade_general值进行过滤。

Alternatively you can set up a different data structure that maps trade_general values to users and jobs. 另外,您可以设置一个不同的数据结构,将trade_general值映射到用户和作业。 Eg 例如

UsersJobTradeGenerals
  KYQ0L5TStcZM1rgVpte
    Construction
      Recent_Job_1: true
      Recent_Job_4: true
    OtherTradeGeneral
      Recent_Job_2: true
      Recent_Job_3: true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM