1

Couldn't found any answers on google docs neither on stack, So I have to ask this.

How do I hit a query with OR condition in it to firebase servers.

Basically, I want to check if Specific chat conversation already exist in the database. Chat can have two types of IDs (steveandjacob OR jacobandsteve), how can I check if either one of them exist in DB?

Here is some code I have wrote.

var currentUserID = localstorage.getItem('currentUserID');
    var chatid = currentUserID+''+anotherUserID;
    var chatid2 = anotherUserID+''+currentUserID;
    var ref = firebase.database().ref('messages/conv').orderByChild('chatid').equalTo(chatid);
    ref.on('value', function(snapshot) {
       if (snapshot.exists())
          alert ("exist");
       else
          alert ("not exist");
    });

How it could be done?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Noman Ali
  • 3,160
  • 10
  • 43
  • 77
  • 1
    Wouldn't it be more convenient to create conversation id where users are in alphabetical order? – marzelin Jul 25 '17 at 13:14
  • 1
    With your current code: you can just attach a second listener for `chatid2`. But it seems like you're modeling as I propose here: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase. The whole purpose of basing the room ID on the user names is that you get an idiomatic room ID. As my linked answer (marzelin) suggest, ensure that the names are in a defined order. – Frank van Puffelen Jul 25 '17 at 13:43

0 Answers0