Home > database >  Why my Firebase firestore data is not showing in android
Why my Firebase firestore data is not showing in android

Time:09-05

I am working on an application where anyone can list their products. I am storing data in Firebase Firestore in nested collection Now I want to retrieve that data and show that on my home screen. Now the data is showing but the problem is that it is showing only when I am login in with that same number through that I list that data into Firebase but when I try to log in with another number the data doesn't show. I want that to show to everyone who logged in to the app. Basically My app is just like OLX where anyone can list anything which shows to everyone.

MY CODE TO RETRIEVE THE DATA

  //CODE TO GET CURRENT ID OR USER
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    //CODE TO GET THE DATA FROM FIREBASE

    DocumentReference uidRef =  firebaseFirestore.collection("listing_details").document(uid);
    CollectionReference roomDetailsRef = uidRef.collection("room_details");
    String doc_id = roomDetailsRef.document().getId();

    
    roomDetailsRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    if (document != null) {
                        RoomsDetails obj = document.toObject(RoomsDetails.class);
                        roomsDetails.add(obj);
                    }
                }
                roomsAdapter.notifyDataSetChanged();
            } else {
                Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
            }
        }
    });

enter image description here enter image description here

CodePudding user response:

You have .document(uid) in your path where UID is User ID of user currently logged in. When you use another phone number, that's a different user.

If you want to fetch room_details documents from all listing_details documents then you can use Collection Group queries like this:

db.collectionGroup("room_details").get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                // ... iterate over all docs and render
            }
        });
  • Related