Nachiketa Vadera
Nachiketa Vadera

Reputation: 165

Firestore Query in Android is behaving unexpectedly

Database Snapshot Image

Here is a piece of code I am using to get things from a collection into my own class object. I've now tried a bunch of things [using DocumentReference, looking up different code available online etc.] but the problem still persists.

I cannot retrieve data which I know is stored in the Firestore. When I execute the code, I get that the QueryDocSnap is empty.

CollectionReference reference = firestore.collection("data");
        reference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                if (queryDocumentSnapshots.isEmpty()) {
                    Log.i(TAG, "QueryDocSnap is empty");
                } else {
                    List<ReportStore> types = queryDocumentSnapshots.toObjects(ReportStore.class);

                    reportStores.addAll(types);
                    Global.setStoreData(reportStores);
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.e(TAG, "Error Getting Data", e);
            }
        });

/app/store/data/4EOi3Eh1AkZf1rK5zwKt is the hierarchy of my database with Firestore telling me that 'app' and 'data' are Collection and the other two are Document.

Could someone please clear out this confusion of mine. Thank you.

Upvotes: 1

Views: 53

Answers (1)

Ashish
Ashish

Reputation: 6919

This code will provide you proper list of documents from data.

CollectionReference reference = firestore.collection("app").document("store").collection("data");
reference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Log.d(TAG, document.getId() + " => " + document.getData());
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

Upvotes: 1

Related Questions