Ad

Retrieve All Subcollections Under Document

I am creating a chat app with react native, There's a collection on my firestore called contacts, under contacts every user's email will be present there, so when I want to get the contacts of a user I'll just make a request to firestore with the users email, anytime I make the request using the code below, the data is always empty, please what may be the issue, This is an image of my database below and my code enter image description here

enter image description here

var docRef = db.collection("contacts").doc(user.email);
docRef.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data()); // doc.data() comes back empty
  } else {
    console.log("No such document!"); // it doesn't get here, document exists
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});
Ad

Answer

Firestore queries are shallow. They don't extend to subcollections. There is no one query that will give you documents from multiple collections (except for collection group queries, which does not apply here).

If you want the documents in a subcollection, you will have to know the name of the subcollection and perform an additional query for its documents.

You are also not able to list subcollections nested under a document unless you are writing code using one of the server SDKs. It's not currently supported for web and mobile clients.

Ad
source: stackoverflow.com
Ad