0
votes

I'm using Firestore and trying to get all the documents under the root collection. I'm facing strange behaviour, I gets only those documents which were I added manually. I tried this, this and many other solutions like these but not getting all the documents.

Here's my Firebase structure

enter image description here

One more thing which is noticeable, the 1st document under users collection which had I added by code looks italic and the other one looks normal & I'm only getting 2nd one in the logcat which is added manually.

Here's my code

FirebaseFirestore.getInstance().collection("users")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.e("===>>>>", document.getId() + " => " + document.getData());
                            }
                        } else {
                            Log.d("===>>>>", "Error getting documents: ", task.getException());
                        }
                    }
                });

Here's my logcat

enter image description here

What is the proper way to get all the doc, does't matter it should be added by code or manually!

2
It's because there is no data inside your document. You have created sub collection in your documentAshish
On Stack Overflow, don't share images with text. Copy the text into the question and format it so that it's easy to read.Doug Stevenson
@Ashish - there's also no data in the 2nd doc except a sub-collection but it is appearing in the console.Irfan Akram

2 Answers

1
votes

the 1st document under users collection which had I added by code looks italic

That means there is no actual document there. What you've done is create a document in a subcollection under than document ID without also creating that document. That's not an error, but you need to be aware that it's also not an actual document and won't show up in queries. It's just shown there so you can find the nested subcollection in the console.

0
votes

data is empty bro thats why you got this problem. okay listen i will share a block of code may this help you when you have data in firestore collection

 FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("matches")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {

                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d("", document.getId() + " => " + document.getData());

                                String actualPlayers = document.get("actualPlayers").toString();
                                String city = document.get("city").toString();
                             String   cooardinates = document.get("coordinates").toString();

 } else {
                            kProgressHUD.dismiss();

                            Log.d("", "Error getting documents: ", task.getException());

                        }

                    }
                });


    }