3

I'm trying to build a query with several where conditions dynamically.

Context : An user select some filters like "brown hair", "tall" etc.. and I want to get in Firebase the corresponding data.

Currently, I have all my filter inside an object :

var whereConditions = {fieldName: "value", fieldname: "value"};

in our case :

var whereConditions = {"hairColor": "brown", "height": "tall"};

With this object, I would like to build the query :

var docRef = db.collection("people").where("hairColor","==","brown").where("height","==","tall")

But I'm facing an issue, I have no idea how many filters the user will choose (so, no idea about the number of where conditions), and I have no idea how to build this query.

I tried something like this, but it's not working :

for (var key in listCondition) {
   whereCond = whereCond + '.where(' + key + ', "==", ' + listCondition[key] + ')';
}

var docRef = db.collection("people").whereCond

I was pretty sure it wouldn't work, but I tried some stuff before coming here for help.

Thanks in advance

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Gio
  • 543
  • 1
  • 5
  • 10

0 Answers0