Adding Server Timestamp field to the Object which being added

后端 未结 4 479
花落未央
花落未央 2020-12-28 08:32

I have a Challenge object, which has it\'s own properties and I\'m able to add it to the database successfully like this:

DocumentReference chal         


        
4条回答
  •  伪装坚强ぢ
    2020-12-28 08:56

    Kotlin

    In kotlin we need to define an initialized data class to send the whole object to Firestore, make sure that you add your @ServerTimeStamp in the last field, since if you add it at first, the construction of your object will need to pass this time as a value to your Object instance.

    data class Order(val orderId:String, val cart:MutableList, @ServerTimeStamp timestamp:Date? = null)
    

    Create your object and send the object values to Firestore

    val order = Order("kJKLkj259ajHHkl",cartList)
    
    FirebaseFirestore.getInstance().collection("orders").add(order)...
    

    This will create your object with orderId, cartlist and also the timestamp which will be stored as

    In my case I'm from Argentina, and the date picked up from my app was this one

    timestamp: 30 de marzo de 2020, 10:12:43 UTC-3
    

    You can find more about date formats here and here

提交回复
热议问题