Disturb
Disturb

Reputation: 598

Can't get subcollection in Firestore - Kotlin

I'm doing a little test app and I'm trying to get all products from a market.

The current database schema is:

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

my code in kotlin is:

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){ }

I also tried replacing the db.collection().document().collection() with:

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

however it stills returns 0 items, ( there are two).

Any help would be appreciated

Upvotes: 0

Views: 1237

Answers (1)

Disturb
Disturb

Reputation: 598

After I tested a lot of variations I noticed that no matter what collection I entered it didn't give me any results, even with collections that worked on other activities.

so I went to application manager on the phone an clear cache and delete data, then I opened again and now is working.

Upvotes: 1

Related Questions