I have a database like this Shop > warehouse > categories > cat1,cat2,..
and every category has a name and its documents. I just want only the categories, and I tried with this code:
firestore.collection("shop/warehouse").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if(queryDocumentSnapshots != null) {
for (DocumentSnapshot doc : queryDocumentSnapshots) {
String item = doc.getId();
Log.d("TEST ITEM", item);
}
} else Log.d(TAG, "queryDocumentSnapshots is null.");
}
});
But as expected I got this error:
Invalid collection reference. Collection references must have an odd number of segments, but shop/warehouse has 2
So I googled and found a solution that suggests to divide in collection/document and I wrote this:
firestore.collection("shop/").document("warehouse").addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
Map<String, Object> map = documentSnapshot.getData();
Log.d("size", map.keySet().size()+"");
}
But this keyset (I assumed they're the category names) is empty. How can I do it? Have mercy on me, it's my first time with Firebase!