AngularFire Database Query

1,454 views
Skip to first unread message

Richard Marais

unread,
Jan 9, 2017, 10:41:10 AM1/9/17
to Firebase Google Group
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.



Note:

I think I am supposed to use equalTo in the query. But not sure how to yet.

Kato Richardson

unread,
Jan 9, 2017, 12:19:42 PM1/9/17
to Firebase Google Group

Hi Richard,

Starting context:

So first off, don't use arrays. Start with something like the following:

/rooms/$chatid/members/aarav => true
/rooms/$chatid/members/mary => true
/messages/$chatid/$messageid => { ..., timestamp }

Now you can query with something like the following:
var authUid = 'aarav';
var chatId = 'room 1';
var ref = firebase.database().ref();

// query to fetch all rooms that Mary is a member of
var roomQuery = ref.child('rooms').orderByChild('members/' + authUid).equalTo(true);

// query to fetch messages for a single room
var messageQuery = ref.child('messages').child( chatId ).orderByChild('timestamp').limitToLast( 50 );
If you're looking to aggregate all messages from any chat that aarav can access, then you want to create a per-user feed and write the messages into each user's feed as they are created, similar to how Facebook and Twitter manage these.

☼, Kato




--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/48e38dc6-4ca9-491b-aaa8-d21964816e9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Kato Richardson

unread,
Jan 9, 2017, 12:20:29 PM1/9/17
to Firebase Google Group
Also see our sample apps.
Reply all
Reply to author
Forward
0 new messages