0

I'm trying to gain an understanding of query in Firebase. I want to pass userId A & B and find out if they are subscribed to a common chatId, it will either return true or false.

How can query both userId and evaluate results for my desired output?

export const checkForExistingChat = (currentUserId, recipient) => {
  var IdList = {}
  var query = database
    .ref(`Chats/${currentUserId}`)
    .orderByChild("subscribedToChat")
    .once("value", function (dataSnapshot) {
      dataSnapshot.forEach(function (childSnapshot) {
        const childData = childSnapshot.val();
        console.log("childData : ", childData);
      });
    });
};

Export JSON of Chat

  "Chats" : {
    "61vtPjp8YVVSzpvexwXMgEHghYf1" : {
      "subscribedToChat" : {
        "1a555cbf-30b7-4c8f-9986-4252a7620c45" : "1a555cbf-30b7-4c8f-9986-4252a7620c45",
        "2d718ea7-eafa-48db-af14-f165f07b3b08" : "2d718ea7-eafa-48db-af14-f165f07b3b08",
        "2e4fd8bb-4afb-4229-83ec-5a427fe2731d" : "2e4fd8bb-4afb-4229-83ec-5a427fe2731d",
        "35c537ef-57dd-48c5-9350-5d1fe2e9d118" : "35c537ef-57dd-48c5-9350-5d1fe2e9d118",
        "3a816ac1-6e97-4d66-ae19-77e65f8c2df4" : "3a816ac1-6e97-4d66-ae19-77e65f8c2df4",
  
      }
    },
    "qqpBNbEa8ZSiCEUlseFeGeiRqzh2" : {
      "subscribedToChat" : {
        "1a555cbf-30b7-4c8f-9986-4252a7620c45" : "1a555cbf-30b7-4c8f-9986-4252a7620c45",
        "35c537ef-57dd-48c5-9350-5d1fe2e9d118" : "35c537ef-57dd-48c5-9350-5d1fe2e9d118"
      }
    }
  }
Freddy.
  • 1,593
  • 2
  • 20
  • 32
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in the overflow menu (⠇) of [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Sep 14 '20 at 14:55
  • @FrankvanPuffelen Ah did not know that. I have now included JSON of Chat. I'm trying to read both given userID `61vtPjp8YVVSzpvexwXMgEHghYf1` and `qqpBNbEa8ZSiCEUlseFeGeiRqzh2` and check if they have a common chatId which should be `35c537ef-57dd-48c5-9350-5d1fe2e9d118` – Freddy. Sep 14 '20 at 15:01

1 Answers1

0

Since you already know how to load data from Firebase, this is essentially a non-Firebase problem: finding the overlapping keys in two lists of keys.

A quick code snippet:

var json = {
  "Chats" : {
    "61vtPjp8YVVSzpvexwXMgEHghYf1" : {
      "subscribedToChat" : {
        "1a555cbf-30b7-4c8f-9986-4252a7620c45" : "1a555cbf-30b7-4c8f-9986-4252a7620c45",
        "2d718ea7-eafa-48db-af14-f165f07b3b08" : "2d718ea7-eafa-48db-af14-f165f07b3b08",
        "2e4fd8bb-4afb-4229-83ec-5a427fe2731d" : "2e4fd8bb-4afb-4229-83ec-5a427fe2731d",
        "35c537ef-57dd-48c5-9350-5d1fe2e9d118" : "35c537ef-57dd-48c5-9350-5d1fe2e9d118",
        "3a816ac1-6e97-4d66-ae19-77e65f8c2df4" : "3a816ac1-6e97-4d66-ae19-77e65f8c2df4",
  
      }
    },
    "qqpBNbEa8ZSiCEUlseFeGeiRqzh2" : {
      "subscribedToChat" : {
        "1a555cbf-30b7-4c8f-9986-4252a7620c45" : "1a555cbf-30b7-4c8f-9986-4252a7620c45",
        "35c537ef-57dd-48c5-9350-5d1fe2e9d118" : "35c537ef-57dd-48c5-9350-5d1fe2e9d118"
      }
    }
  }
};
var keys1 = Object.keys(json.Chats["61vtPjp8YVVSzpvexwXMgEHghYf1"].subscribedToChat);
var keys2 = Object.keys(json.Chats["qqpBNbEa8ZSiCEUlseFeGeiRqzh2"].subscribedToChat);

console.log(keys1, keys2);
var commonKeys = keys1.filter(function(key) {
  return keys2.indexOf(key) >= 0;
});
console.log(commonKeys);

This is an O(n^2) algorithm, but I doubt that'll be a concern on the list sizes you're likely to have. If it is a concern, and the lists are sorted, you can keep a cursor in each least, and move forward only through them once to make it an O(2n) algorithm.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your answer, I think the issue I am struggling with is how to store the retrieved data from DB in a variable, to be evaluated on the client-side. From the function I posted, how can I save `childData` to be scoped within the method so that I can compare user A & B array to filter commom Id. I have added `var IdList = {}` but it always seems to be empty – Freddy. Sep 14 '20 at 17:29
  • This was the original approach I was going to take but not sure how to put query results into a variable keys1 and keys2. I tried `IdList = childSnapshot.val();` but it's empty – Freddy. Sep 14 '20 at 17:45
  • Most likely you're accessing `idList` when the line you included hasn't run yet. All code that needs data from the database needs to inside the callback or be called from there. See https://stackoverflow.com/questions/63144411/how-to-get-value-of-a-users-child-from-firebase-real-time-database-with-exporte/63145021#63145021 and the links from there. This has nothing to do with intersecting two lists though. You'd get the same problem with any asynchronous calls. – Frank van Puffelen Sep 14 '20 at 18:26
  • Thanks for all your help Frank, I think I have an idea what to do – Freddy. Sep 14 '20 at 18:37