2

I have a Firestore database where I'm trying to query some user information via the Android API, but it doesn't seem to return results.

The structure of my database looks like this:

/users/test_user/test_sub_collection/randomDocId

(main collection -> doc -> sub collection -> doc)

Here's the Java code:

FirebaseFirestore.getInstance()
        .collection("users")
        .document("test_user")
        .collection("test_sub_collection")
        .get()
        .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    List<DocumentSnapshot> documents = task.getResult().getDocuments();
                    for (DocumentSnapshot doc: documents) {
                        if (doc.exists()) {
                            // And here lies the problem. The getData size is always zero, 
                            // doesn't matter how many fields and subcollections 
                            // I keep adding to the randomDocId.
                            doc.getData();
                        }
                    }
                }
        });

So to sum up, doc.getData().size() always returns 0, even though doc.getId() returns the correct id (randomDocId). randomDocId also has a field and sub collection.

I honestly don't have a clue what I'm doing wrong.

Community
  • 1
  • 1
Cerberos
  • 91
  • 1
  • 6
  • FYI: `getData()` won't tell you anything about subcollections, only fields of the document. – Doug Stevenson Oct 02 '18 at 18:40
  • So basically you want to get the number of documents within your `test_sub_collection`? Please responde with @. – Alex Mamo Oct 03 '18 at 11:58
  • @AlexMamo No. I actually have one document in `test_sub_collection` with the ID of `randomDocId`. I try to retrieve this document in the lambda function you can see above, but it doesn't work as expected. The `doc.exists()` piece returns true and I can even get the ID of the document, but when I try to retrieve its fields and collections, it doesn't return anything. – Cerberos Oct 04 '18 at 16:11
  • @Cerberos Can you please add a screenshot of your database structure to see the schema more clearly? – Alex Mamo Oct 04 '18 at 16:18

0 Answers0