【发布时间】:2020-05-01 19:35:29
【问题描述】:
我无法使用以下代码从 Firestore 中的新集合。它总是给我错误:
集合引用无效。集合引用必须有奇数个段,但 Stores/Stored_Product_Storage 有 2 个。
ProductClass dummyProduct = new ProductClass("dummyitem", 0, "none", 0);
db.collection("Stores").document("Stored_Product_Storage").collection(String.valueOf(docLocation)).add(dummyProduct);
db.collection("Stores").document("Stored_Product_Storage").collection(String.valueOf(docLocation))
.whereGreaterThan("price", 0)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
products.clear();
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
String name = document.get("name").toString();
int price = Integer.parseInt(document.get("price").toString());
float units = Float.parseFloat(document.get("units").toString());
String unitType = document.get("unitType").toString();
ProductClass product = new ProductClass( name, price, unitType, units);
products.add(product);
}
progressDialog.dismiss();
storeProductUpdateActivityAdapter.notifyDataSetChanged();
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
当我直接插入一个字符串而不是 String.valueOf(docLocation) 时,会创建一个新集合以及一个文档来插入虚拟产品数据。但是,如果我尝试使用变量而不是直接在该字段中给出字符串,那么它会给我集合引用错误。读取数据时也会发生同样的情况。我在 docLocation 中存储了一个自定义字符串 ID。有人可以帮我解决这个问题吗?提前致谢。
【问题讨论】:
标签: java android firebase google-cloud-firestore