Firebase Database Query (AngularFire)


#1

I am using Ionic 2 and Firebase to build a chat app.

I have the following code, that creates an entry:

import {
  FirebaseAuth,
  AngularFire,
  FirebaseListObservable
} from 'angularfire2';

    public af: AngularFire
            ...
        let memberIds: string[] = [senderId, receiverId];
            ...
        this.af.database.list('/chat/').push({
          memberIds: memberIds,
          negativtimestamp: -Date.now()
        })

As you can see, it adds a chat for the specified memberIds made up of a senderId and a receiverId.

The following code, returns a list of all the chats.

this.firelist = this.af.database.list('/chat/', {
  query: {
    orderByChild: 'negativtimestamp',
  }
});

Now, I want to only be able to retrieve the chats for specified memberIds.

Question

How do I retrieve a list of chats for only a specified senderId or receiverId? i.e. The chats that contain either a matching senderId or receiverId in the memberIds.

Any help appreciated.

UPDATE

I add the following which is supposed to be used to add a filter:

import 'rxjs/add/operator/filter';
        ...
    this.firelist = this.af.database.list('/chat/', {
      query: {
        orderByChild: 'negativtimestamp'
      }
    }).filter(item => item.memberIds === 'xxx');

However, I get the following:

[ts] Property ‘filter’ does not exist on type
’FirebaseListObservable<any[]>’.

enter image description here


#2

How did you solve this?


#3

AngularFire uses its specific observable which doesn’t contains all methods of rxjs. That’s why filter() is not found.

Anyway to filter a result you need to add “equalTo” to your query object and set the field you want to search in “orderByChild”

this.firelist = this.af.database.list('/chat/', {
  query: {
    orderByChild: 'senderId ',
    equalTo: 'yourFilterValueHere'
  }
});

Check official doc for more details : https://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.md