I was having some problem with Firebase. What I wanted to do is create a new chatroom if it does not exist, otherwise, get the existing chatroom ID. What I have achieved so far is:
The database structure as such:
chatrooms
chatroomID
participantID1 *(can be senderKey or recipientKey)*
participantID2 *(can be senderKey or recipientKey)*
latestMessage
The code where I create the new chatroom:
let promiseRoomKey = new Promise((resolve, reject) => {
// need to perform check before insert new chatroom
// create room for chat
var roomKey = firebase.database().ref('chatrooms').push({
[senderKey]: true,
[recipientKey] : true
}).getKey();
resolve(roomKey);
});
The problem is before I create the new chatroom, I need to check if both the senderKey and recipientKey existed in the same chatroom. If both of them appeared in the same chatroom, I grab the chatroomID. If not, then I will proceed to create a new chatroom.
But I have no ideas how can I actually check if both the senderKey and recipientKey appeared in the same chatroom. Any ideas?
Ps. Please ignore the latestMessage node as of now because it is meant to ease my reading purpose only.
Thanks!