Firestore更新文档中的数组字段

时间:2018-06-12 23:14:42

标签: firebase kotlin google-cloud-firestore

我有一个文档,如下所示,在我的文档中,有一个数组。在数组中有对象。如何将新对象更新为数组。

如下所示,我可以在一个数组的集合中添加文档,但是当我尝试更新它时会出错

java.lang.IllegalArgumentException:数据无效。不支持的类型:com.test.data.modal.Product

我尝试了什么;

        var pid = ""
        btnAdd.setOnClickListener {
            val list = ArrayList<Product>()
            list.add(Product("u1", "1", 1))
            list.add(Product("u2", "2", 1))
            list.add(Product("u3", "3", 1))

            val testObject = TestObject("Talha", "Kosen", list)
            FirebaseFirestore.getInstance().collection("Test")
                    .add(testObject)
                    .addOnCompleteListener { task ->
                        pid = task.result.id
                    }
        }

        btnUpdate.setOnClickListener {
            val list = ArrayList<Product>()
            list.add(Product("u1", "1", 1))
            list.add(Product("u2", "2", 1))
            list.add(Product("u3", "3", 1))
            list.add(Product("u4", "4", 4))

            FirebaseFirestore.getInstance()
                    .collection("Test")
                    .document(pid)
                    .update("product", list)
        }

文件:

enter image description here

POJO:
@IgnoreExtraProperties
@Keep
class TestObject {

    var name: String = ""
    var surname: String = ""
    lateinit var productList: List<Product>

    constructor()

    constructor(name: String, surname: String, productList: List<Product>) {
        this.name = name
        this.surname = surname
        this.productList = productList
    }
}


@IgnoreExtraProperties
@Keep
class Product {
    var imagePath: String = ""
    var productUrl: String = ""
    var brand: Int = 0

    constructor()

    constructor(imagePath: String, productUrl: String, brand: Int) {
        this.imagePath = imagePath
        this.productUrl = productUrl
        this.brand = brand
    }
}

1 个答案:

答案 0 :(得分:0)

我可能没有答案,但这可能会有所帮助: