Guy Ohm
Guy Ohm

Reputation: 89

Firestore : query all documents from a collection

My Firestore structure looks something like this:

-(coll)users
    -(doc)uniqueID  
    name
    email  
    (coll)clients
        -(doc)uniqueID
        clientName
        clientEmail  

What I am trying to achieve is the following:

  1. The user signs in through FirebaseUI Auth flow
  2. If the user (uid recovered from Auth) doesn't exist in firestore db I create a document named by his uid
  3. Once I have the uid I run a query to load clients collection in order to display them into a list using a RecyclerView (if the collection is empty hence the user hasn't created any clients yet I display an empty list screen)

I tried to make a query using the code below as per the documentation:

    clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
            .document(mUid)
            .collection(FIRESTORE_COLLECTION_CLIENTS);

    clientsCollection
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()){
                        for (DocumentSnapshot document: task.getResult()){
                            Log.d(LOG_TAG, document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d(LOG_TAG, "error getting documents: ", task.getException());
                    }
                }
            });

I get the following RuntimeException:

java.lang.NullPointerException: Provided document path must not be null.

I get this even if the clients collection exits with some documents in it named by unique uid.

Thanks for any clue you could give me! :)

Upvotes: 3

Views: 3891

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600110

The error message indicates that mUid is null when you run the first statement. Most likely this means that you run this code before the user is signed in.

Make sure that you only call this code after the user has signed in, e.g. from an AuthStateListener.onAuthStateChanged():

FirebaseAuth.getInstance().addAuthStateListener(new AuthStateListener() {
    public void onAuthStateChanged(FirebaseAuth auth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
                    .document(user.getUid())
                    .collection(FIRESTORE_COLLECTION_CLIENTS);

            clientsCollection
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()){
                                for (DocumentSnapshot document: task.getResult()){
                                    Log.d(LOG_TAG, document.getId() + " => " + document.getData());
                                }
                            } else {
                                Log.d(LOG_TAG, "error getting documents: ", task.getException());
                            }
                        }
        }
    }
})

Upvotes: 2

Related Questions