0
votes

Here is my realtime db structure

myproject
|---------event
              |--date1
                  |--adminid1
                       |-----hallID1
                               |--name:"abc"
                               |--address: "xyz"
             |--date2
                 |--adminid1
                      |-----hallID2
                              |--name:"asd"
                              |--address: "somenadd"

I am trying to an autocomplete search box for name element of my json structure, whenever user enters more than three characters in my search box , I call the search() function which queries firebase db as follows:

scope.search = function(){
        firebaseDb
        .ref("event")
        .orderByChild("name")
        .startAt(scope.searchBarModel) 
        .endAt(scope.searchBarModel + "\uf8ff")     
        .on("child_added", function(snapshot){
            console(snapshot.key);
        });     
    }

However, the above query is not able to find the records in db , what am I doing wrong?

PS: Also same is the case with equalTo query :

scope.search = function(){
        firebaseDb
        .ref("event")
        .orderByChild("name")
        .equalTo(scope.searchBarModel) 
        .on("child_added", function(snapshot){
            console(snapshot.key);
        });     
    }
1
Change "child_added" to value. Child added only fires when someone writes to that area of the database. - DoesData

1 Answers

1
votes

As stated in my comment you just need to change "child_added" to "value". The reason for this is because "child_added" will only fire when new data is written to that node. Since you want existing data "value" is what you want to use. You also might want to use .once() instead of on() so that the listener is detached. You can read more here