Keval Patel
Keval Patel

Reputation: 995

Multiple orderByChild filter in firebase database query

I want to filter data from firebase database in angular 4 application. I need to apply two filters.

  1. Fetch all onLine users
  2. Fetch all not playing users

I read few articles that, We cannot add two orderByChild critearea in query. So I used map function as below,

users: FirebaseListObservable<any[]>;
public DB:
AngularFireDatabase;

constructor(private router: Router, private db: AngularFireDatabase) {
  this.DB = db;
}
showPeopleList() {

  this.users = <FirebaseListObservable<any>>this.DB.list('/users',
  {
    query: {
      orderByChild: 'isOnline',
      equalTo: true,
      limitToLast: 20
    }
  }).map((items)=> { 
    let filteredUser: any[];
    for (let item of items) {
      if(items.isPlaying == false){
        filteredUser.push(item);
      }
    }
    return filteredUser;
});



}

In above code, in map function, I am getting all Online users, but I am not able to filter data based on Playing field. What changes do I need t make to get this done.

Upvotes: 0

Views: 1696

Answers (1)

Mitta
Mitta

Reputation: 453

You should be able to just use the .filter function for this.

.filter(item => item.isPlaying === false)

In your above code you have a spelling error which also might cause your current function not to work. Notice the s in after items in your if statement.

 if(items.isPlaying == false){
        filteredUser.push(item);
 }

Upvotes: 3

Related Questions