【问题标题】:Getting Invalid collection reference error when accessing Firestore访问 Firestore 时出现无效的集合引用错误
【发布时间】: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


    【解决方案1】:

    几乎可以肯定String.valueOf(docLocation) 返回一个空字符串。在将其传递给 document() 之前,您需要进行检查。

    【讨论】:

    • 是的,我实际上在此处发布了 10 分钟,但后来忘记在此处将其标记为已修复。是的,docLocation 没有任何数据。我将它设置为接收我之前存储在共享首选项中的数据,但结果它是空的,这是由于另一个错误。所以我修复了它。谢谢你的回复。顺便说一句,因为我是新手,我对 stackoverflow 了解不多,但为什么我投了反对票?是因为答案很简单吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 2018-08-06
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    相关资源
    最近更新 更多