简体   繁体   中英

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.

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());
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM