0

I am checking if a combination of child values exists in my DB. This works but can't get the proper message to be displayed in the console. the correct console message is displayed in both else statements. The one that is not being displayed properly is the console.log('yes'). Or actually it is displayed but always followed by aconsole.log('no') and I have no idea why

There is however a match found because the correct data is shown in console.log(details); console.log(snapshot.key);

FBSearch: function(){
    var T1 = this
    var T2 = this
    var T3 = this
    var ref = firebase.database().ref("flights");
    ref
    .orderByChild('date')
    .equalTo(T2.flight.dat)             
    .on('child_added', function(snapshot) {
        var details = snapshot.val();
        if (details.flight == T1.flight.flt) {
            if(details.origin == T3.flight.org){
                console.log(details);
                console.log(snapshot.key);
                console.log('yes')
            } else {
                console.log('no')
            }           
        } else {
            console.log('no')
        }                   
    })
}

The desired outcome is a success message when a match is found

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MK01111000
  • 770
  • 2
  • 9
  • 16
  • Can you edit your question to include a snippet of the JSON at `flights` (as text, no screenshots)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 28 '19 at 14:19

1 Answers1

1

Not sure if this is the answer to your question, but something you might want to consider...

Right now you're reading all flights of the specific date, in an effort to check if one of them exists. This is suboptimal, and can waste lot of your users' bandwidth. Ideally a yes/no question like that should require you to pass in the criteria, and get a minimal yes/no type answer.

While the Firebase Realtime Database doesn't support exists() queries, and can't query on multiple conditions, you can make significant improvements by adding a special property to your data to support this query.

As far as I can see, you're filtering on three properties: date, flight, and origin. I recommend adding an extra property to all flights that combines the values of these properties into a single value. Let's call this property "origin_date_flight".

With that property in place you can run the following query to check for the existence of a certain combination:

ref
.orderByChild('origin_date_flight')
.equalTo(`${T3.flight.org}_${T2.flight.date}_${T1.flight.flt}`) 
.limitToFirst(1)
.once('value', function(snapshot) {
    if (snapshot.exists()) {
        console.log('yes')
    } else {
        console.log('no')
    }           
})

The main changes here:

  • The query uses a composite property as a sort-of index, which allows it to query for the specific combination you're looking for.
  • The query uses limitToFirst(1). Since you're only looking to see if a value exists, there's no need to retrieve multiple results (if those exist).
  • The query uses once("value", which allows it to check for both existence and non-existence in a single callback. The child_added event only fired if a match exists, so can't be used to detect non-existence.

And as a bonus, this new property also allow you to for example get all flights out of Amsterdam today:

ref
.orderByChild('origin_date_flight')
.startAt(`AMS_20190728_${T2.flight.date}_`) 
.endAt(`AMS_20190728_${T2.flight.date}~`) 
.once('value', function(snapshot) {
    snapshot.forEach(function(flightSnapshot) {
        console.log(flightSnapshot.key+": "+flightSnapshot.child("flt").getValue());
    })
})

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This looks promising. I'll give it a try when I am back at my desk again. Thanks for the detailed explanation, it's really appreciated. I will share the outcome. (AMS is my favorite airport as well :) – MK01111000 Jul 28 '19 at 15:10