I am using Google's Firestore for searching a database, and the logic is to name your source, then chain where() methods for each variable. An example of working code is:
var ref = firebase.firestore().collection('myCol');
ref.where('myVar1','==',true).where('myVar2','==',5).get()
.then((results) => {...})
The problem I'm having is I have no idea how to dynamically attach those where() methods (as the number of them will change with each different search). I suspect if I knew the name of it I'd be able to find it, but dot functions didn't show up much... How could I do this?
I am using Google's Firestore for searching a database, and the logic is to name your source, then chain where() methods for each variable. An example of working code is:
var ref = firebase.firestore().collection('myCol');
ref.where('myVar1','==',true).where('myVar2','==',5).get()
.then((results) => {...})
The problem I'm having is I have no idea how to dynamically attach those where() methods (as the number of them will change with each different search). I suspect if I knew the name of it I'd be able to find it, but dot functions didn't show up much... How could I do this?
Share Improve this question edited Oct 27, 2017 at 20:18 xon52 asked Oct 26, 2017 at 10:00 xon52xon52 7365 silver badges13 bronze badges 3- What are "dot functions"? – Tomalak Commented Oct 26, 2017 at 10:04
-
3
Chaining is often just a case where the method returns itself, or another object. So just a keep a reference to this.. eg..
var last = ref.where(..);
, thenlast = last.where(..)
etc. – Keith Commented Oct 26, 2017 at 10:04 - 6 Please don't use the question to provide an answer to it. Instead write your answer below. You can accept it in 2 days as well. If you are looking for an answer, the question is definitely not the first place you are looking for it ;) – geisterfurz007 Commented Oct 26, 2017 at 11:56
1 Answer
Reset to default 6From @Keith's reply below, I got it working using:
var vars = ['myVar1', 'myVar2', 'myVar3'];
var ref = firebase.firestore().collection('myCol');
vars.forEach(v => { ref = ref.where(v, '==', true) });
ref.get()
.then(results => { ... })
.catch(err => { ... })