android - 由于错误,无法在Firestore中更新阵列

标签 android firebase kotlin google-cloud-firestore

我在Kotlin中有以下数据类:

data class Product(
    var id: String? = null,
    var name: String? = null,
    @ServerTimestamp
    var createdAt: Date? = null
): Serializable
而这个Firestore数据库:
Root
 \
 carts <- collection
   \
   cartId <- document
     \
     products: [productObj, productObj, productObj]
我正在尝试将另一个Product对象添加到列表中。我正在使用以下行:
cartsRef.document(cartId).update("products", FieldValue.arrayUnion(product)).addOnCompleteListener {}
我收到以下错误:

java.lang.IllegalArgumentException: Invalid data. FieldValue.serverTimestamp() can only be used with set() and update()


为什么由于使用update()函数而出现此错误?

最佳答案

在您的数据类中,您应该尝试将@ServerTimestamp更改为

@ServerTimestamp private val serverTimestamp:Date

关于android - 由于错误,无法在Firestore中更新阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62814364/

相关文章:

android - 在相对布局中使用 layout_above

android - 在 android studio 中,Gradle 同步失败访问被拒绝

firebase - Google Firestore - 是否存在小型业务?

android - Firebase 实时数据库 : Using Firebase Realtime Database in always running service in Android

kotlin - 为什么我不能说:val list = properties.map((key,value)-> “$key = $value”)

kotlin - Kotlin + Intelij + HtmlUnit:使用提供的参数无法调用以下函数

android - admob中的Mediation id和publisher id有什么区别

FragmentActivity 中的 Android admob

ios - 随机化 Firebase 存储中的图像

kotlin - 如何在没有 NoSuchMethodError 的 Kotlin 1.4 的 Gradle 构建中使用新定义的委托(delegate)属性?