最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Dynamically chain methods to javascript function - Stack Overflow

programmeradmin1浏览0评论

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(..);, then last = 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
Add a ment  | 

1 Answer 1

Reset to default 6

From @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 => { ... })
发布评论

评论列表(0)

  1. 暂无评论