2

I have this use case where I have to query based on the parameters sent out by the user in the form of an object. The user could send out multiple parameters to query. It is something similar to what "SELECT * FROM users WHERE FIRST_NAME = 'something' AND LAST_NAME = 'something'" is in SQL.

A sample object could be:

var object= {
    email: "some...@google.com",
    location: "San Jose, CA"
};

I have these fields (email & location) in my firebase data at some endpoint lets call it /users

So the users endpoint would look like:

{
  "randomID1":{
    email: "some...@google.com",
    location: "San Jose, CA"
  },
  "randomID2":{
    email: "anothe...@fb.com",
    location: "Menlo Park, CA"
  }
}

I have to use the above mentioned object and generate a query dynamically for firebase, here's what I have:

return $q(function(resolve, reject) {
            ref.orderByChild("email");
            for(var key in filterObject){
                if(filterObject.hasOwnProperty(key)){
                    console.log("Key: ",key);
                    console.log("Value: ",filterObject[key]);
                    ref.equalTo(""+filterObject[key],""+key);
                }
            }
            return ref.on("value", function (snapshot) {
                resolve(snapshot.val());
            }, function (errorObject) {
                reject(errorObject);
            });
        });

This always returns me all the data and doesn't really filter anything. Can anyone provide suggestions here? I am new to firebase , sorry if this is a naive question.

Thanks

Rohan Dalvi
  • 1,215
  • 1
  • 16
  • 38
  • You're ordering data on the Firebase server, but you're not filtering it there. You'l need to add something like `equalTo()`. Read this page of the Firebase docs for all the info: https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries. – Frank van Puffelen Oct 23 '15 at 01:25
  • 2
    @FrankvanPuffelen Thanks for your response. I think what I am really looking for is this http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Rohan Dalvi Oct 23 '15 at 01:26
  • But note that the Firebase server only allows querying on a single property. See http://stackoverflow.com/questions/30654872/how-to-query-based-on-multiple-conditions-in-firebase – Frank van Puffelen Oct 23 '15 at 01:26

1 Answers1

1

After some attempts, I found an answer. Basically, Firebase does not allow to query on more than a single property so I query firebase on the first property and if there are any more properties to still query, I use underscore js to filter on those remaining properties.

Code looks like this:

return $q(function(resolve, reject) {
            var query = ref ;
            for(var key in filterObject){
                if(filterObject.hasOwnProperty(key)){
                    query = query.orderByChild(key);
                    query = query.equalTo(filterObject[key]);
                    delete(filterObject[key]);
                    break; //break out after querying the first property in firebase
                }
            }
            return query.on("value", function (snapshot) {
                var objects = snapshot.val();
                var result = _.filter(objects, function(obj){
                    return _.isMatch(obj, filterObject); //for all other objects I use underscorejs to filter
                });
                resolve(result);
            }, function (errorObject) {
                reject(errorObject);
            });
        });
Rohan Dalvi
  • 1,215
  • 1
  • 16
  • 38