Donnie Darko
Donnie Darko

Reputation: 519

Subscribe to Firebase Join Query React Native

As you can see I have 2 Main branches

Users

Conversations

Every User has many conversations in the users branch

I would like to perform a JOIN query so i only subscribe only one time by the user id

So if there is any change in any conversation of the user the callback will be called

enter image description here

Upvotes: 0

Views: 635

Answers (1)

Jordan Daniels
Jordan Daniels

Reputation: 5304

Since firebase db is a NOSQL database, you can't use a join query.

What you can do in your case, is get the conversationId for a user, set it to a variable, and use that variable to make a listener to listen for changes in the conversation.

For example, you can do something like this:

getConversationId(userId, memberId){

  let conversationRef = firebaseDB.ref(`/users/${userId}/conversations/${memberId}`);

  conversationRef.once("value").then(snapshot => {
    if (snapshot.val()){
      let id = snapshot.val()
      let { conversationId } = id;
      this.listenConversation(conversationId);
    }
  })
}

listenConversation(conversationId){

  let conversationsRef = firebaseDB.ref(`/conversations/${conversationId}`);

  conversationsRef.on("value", snapshot => {
    // here is your data
  })
} 

Upvotes: 1

Related Questions