Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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! :)

like image 501
Guy Ohm Avatar asked Dec 10 '25 01:12

Guy Ohm


1 Answers

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());
                            }
                        }
        }
    }
})
like image 60
Frank van Puffelen Avatar answered Dec 11 '25 16:12

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!