javascriptfirebasereact-nativegoogle-cloud-firestore

Firebase Query Builder and Code get data not showing data


I am a React Native coder here I am getting a illogical problem as I am adding 2 screenshots of Firestore in that a collection is there and has some documents. When i get cthe ollection using code or query builder i am getting. Only 1 item still not able to get what is the problem. My React Native code is

useEffect(() => {
    console.log('Starting useEffect');

    const fetchData = async () => {
      const querySnapshot = await firestore()
      .collection('chatRooms')
      .get();

    console.log('All docs count:', querySnapshot.size);

    console.log('All document IDs:');
    querySnapshot.forEach(doc => {
      console.log(`${doc.id}`);
    });

    // Filter client-side for rooms containing the user
    querySnapshot.forEach(doc => {
      const data = doc.data();
      console.log(`Doc ${doc.id} exists:`, doc.exists);
      console.log(`Doc ${doc.id} data:`, data);
      console.log(`Doc ${doc.id} data keys:`, Object.keys(data));
      console.log(`Doc ${doc.id} empty?:`, Object.keys(data).length === 0);

    });


    };
    fetchData();
  }, []);

enter image description here enter image description here

As shown i have 4 documents but getting 1 only. Please let me know if somebody knows about this issue.

HvN0az1wtzgNqnxkBm14z274LiO2_ai_1
HvN0az1wtzgNqnxkBm14z274LiO2_ai_10
HvN0az1wtzgNqnxkBm14z274LiO2_ai_12

My Doc id's


Solution

  • I am adding 2 screenshots of Firestore, in which a collection is present and has some documents.

    The "chatRooms" collection doesn't have "some documents", but only one document, the first one. So what you did was to create a document, which in turn contains a sub-collection called "messages", which contains 7 documents. The other document simply doesn't exist. I know that because the other 3 documents are written in italic, which means that you have created a sub-collection within them without creating the parent document first, hence that result.

    When you programmatically generate an ID for a document, it doesn't mean that you create the actual document in Firestore. Please take into consideration that Firestore documents and subcollections don't work like filesystem files and directories you might be thinking. If you create a sub-collection under a document, it doesn't implicitly create any parent documents. Sub-collections are not tied in any way to a parent document.

    So you see those 3 documents that way, because the Firebase Console indicates that there is some data under the location, so you can navigate further. That's it. If you want to get all the documents within the "chatRooms" collection, then create them, and then the Console and the code in your project will return the expected result.