简体   繁体   中英

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.

I can't get the orderByChild query / filter to work. 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:

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. I have the snippet at the bottom of my html page and I've been reading / writing records with no issue.

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. 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.

But most likely Recent_Job_1 is a dynamic path. 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's indexes are essentially single-value maps and this type of query wouldn't work with those.

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. That would allow you to filter by the latest job's trade_general value.

Alternatively you can set up a different data structure that maps trade_general values to users and jobs. Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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