2018-05-05

Firestore: Reading Document Issue - Document Can Not Be Found


For my Android App I am using Googles' Firebase.Firestore NOSQL database in order to store some data in a cloud DB.

I has no problem to save the data, but retrieving was not working.
So I reduced my test data structure to a minimum and tried all methods described in the documentation without success.

My - not working - minimal test data structure was:



This structure represents a collection of tests ("Tsts") of my App, containing one document for each version of my App, in this case version 11.
The document 11 contains a collection ("Devs") of devices for each device on with my app has been tested.

The solution of the problem was adding a field to the document 11, for example like this:



For testing how to read the data, I used the following code based on the Firestore documentation (Sub-chapter: Get a document).
DocumentReference docRef1 = mDB.collection("Tsts").document("11");
docRef1.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                mLog.debug("DocumentSnapshot data: {}", document.getData());
            } else {
                mLog.debug("No document");
            }
        } else {
            mLog.debug("get failed with ", task.getException());
        }
    }
});

Only after adding the test field "field1", I could read something.
Before adding field1, I always got the message "No document" in the logs.

I hope this info helps someone.
[] 

No comments :

Post a Comment