Query Angular2Firebase for multiple values โ€‹โ€‹where IN

So, I am looking for the equivalent of WHERE IN(val1,val2) I tried this, but it does not work.

Angular firebase

 this.firebase.database.list('/favorites', { query: { orderByChild: 'uID', equalTo: [1,2,3,'some value] } }); 
+5
source share
3 answers

Firebase queries do not have an OR or IN statement. Thus, you cannot easily match the above request with Firebase.

One way would be to implement multiFilter on elements:

 public items: FirebaseListObservable<any[]> = null; ngOnInit() { this.items = this.db.list('/favorites', { query: { orderByChild: 'uID' } }); this.items.subscribe(items => { console.info(this.multiFilter(items,{'uID': [1,2,3,'some value]})); }); } public multiFilter(arr: Object[], filters: Object) { const filterKeys = Object.keys(filters); return arr.filter(eachObj => { return filterKeys.every(eachKey => { if (!filters[eachKey].length) { return true; // passing an empty filter means that filter is ignored. } return filters[eachKey].includes(eachObj[eachKey]); }); }); }; 
+2
source

Queries are created by creating based on firebase.database.Reference. Check out the Doc: https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

  this.firebase.database.list('/favorites', { ref => ref.orderByChild('uID').equalTo(1,2,3,'some value) }); 

or

  constructorpublic af:AngularFireDatabase) { this.items = af.list('/messages', ref => ref.orderByKey(true).equalTo(1,2,3,'some value)); }); 
+1
source

I would have users under node users.

In general, identifiers would be firebase push keys.

therefore user> userId> {user information}.

Then create a business

business> businessId> {business information}

then the business for the node user.

businessForUser> userId> businessId = true

So, then you get a list of business identifiers for a user from a businessForUser node, and then get business information from a business node with the specified identifiers.

+1
source

Source: https://habr.com/ru/post/1275424/


All Articles