【发布时间】:2018-11-07 12:26:43
【问题描述】:
我有一个像Shop > warehouse > categories > cat1,cat2,.. 这样的数据库,每个类别都有一个名称和它的文档。我只想要类别,我尝试使用以下代码:
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.");
}
});
但不出所料,我收到了这个错误:
Invalid collection reference. Collection references must have an odd number of segments, but shop/warehouse has 2
所以我用谷歌搜索并找到了a solution that suggests to divide in collection/document,然后我写了这个:
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()+"");
}
但是这个键集(我假设它们是类别名称)是空的。我该怎么做?可怜我吧,这是我第一次使用 Firebase!
【问题讨论】:
-
@GastónSaillén 这不是重复的,我已经尝试过该问题下面的解决方案,但它们没有用
标签: java android firebase google-cloud-firestore