无法在Firestore中获得子集合 - Kotlin

时间:2018-06-07 16:36:43

标签: firebase kotlin google-cloud-firestore

我正在做一个小测试应用,我试图从市场上获得所有产品。

当前的数据库架构是:

markets:[
       market1: {}, 
       market2: {}, 
       market3: {
          name: "",
          products: [
               item1: {}, 
               item2: {}
          ]
       }
   ] 

我在kotlin的代码是:

try {
     db.collection("markets").document(marketId).collection("products")
                .get()
                .addOnCompleteListener { task ->
                    if (task.isSuccessful) {

                        var products = mutableListOf<Product>()

                        for (document in task.result) {
                            products.add(
                                Product(
                                        document.id,
                                        document.get("name").toString(),
                                    true
                                )
                            )
                        }

                        //updateList(products)


                    } else {
                        Log.e("Getting markets", "Error getting documents.", task.exception)
                    }

                }
    }catch (e : Exception){ }

我还尝试将db.collection().document().collection()替换为:

db.collection("markets/$marketId/products")

然而它仍然会返回0项,(有两项)。

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

在我测试了很多变化之后,我注意到无论我输入什么样的集合,它都没有给我任何结果,即使是用于其他活动的集合。

所以我在手机上通过应用程序管理器清除缓存并删除数据,然后我再次打开,现在正在运行。