Augusto
Augusto

Reputation: 4243

Firebase Firestore returning no data from collection

I want get all docs from a collection but on my code nothing is returned like I can see on the log printing the snapshot returned. I can use Storage on my application to store documents, and I can write on Firestore documents too, so the Firebase is correct configured, but it's not reading the docs.

getUsers() {
  firebase
    .firestore()
    .collection("users")
    .get()
    .then(function(querySnapshot) {
      console.log(querySnapshot);
      querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
      });
    });
}

enter image description here enter image description here

Upvotes: 0

Views: 1503

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

This is working as expected. The query you're performing is only going to get documents immediately in the users collection. However, the screenshot her shows that there are not actually any documents in that collection. The document IDs you see in that collection are in italics, which means there is no document there, HOWEVER, there is at least one subcollection ("data") under that document.

Firestore queries are shallow, meaning that they only consider documents immediately within the collection you're trying to query. Documents in subcollections are not considered. Missing doucuments that have nested subcollections are also not considered (since they are not actually documents).

If you want to be able to query documents in a collection, you must actually write a document there. Documents in subcollections do force a parent document to exist.

Upvotes: 1

Related Questions