1

I have this data class in Kotlin:

data class Product(
    var id: String? = null,
    var name: String? = null,
    @ServerTimestamp
    var createdAt: Date? = null
): Serializable

And this Firestore database:

Root
 \
 carts <- collection
   \
   cartId <- document
     \
     products: [productObj, productObj, productObj]

And I'm trying to add another Product object to the list. I'm using this line:

cartsRef.document(cartId).update("products", FieldValue.arrayUnion(product)).addOnCompleteListener {}

And I get the following error:

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

Why do I get this error since I am using update() function?

Ivan Patrice
  • 119
  • 9

2 Answers2

0

In your Data class you should try changing the @ServerTimestamp to

@ServerTimestamp private val serverTimestamp:Date
maniSidhu98
  • 527
  • 2
  • 9
  • What else have you tried? There's also [this](https://stackoverflow.com/questions/57534290) that may also be of help. – maniSidhu98 Jul 17 '20 at 02:26
0
@ServerTimestamp private val yourDate: Date? = null

Key points are that it has to be private and nullable with default value null, or if not default, set null in constructor

Antonis Radz
  • 3,036
  • 1
  • 16
  • 34