简体   繁体   中英

How to add server TimeStamp in Firestore document android

After searching for some references. when adding field value in data class is enough to create a timeStamp in firestore document. but in my case i did the same

data class

import com.google.firebase.firestore.ServerTimestamp
import java.sql.Date

data class Banner(
 var banner_id:String? = null,
 var banner_title:String? = null,
 var banner_offer:String? = null,
 var banner_image:String? = null,
 var banner_priority:String? = null,
 var banner_tags:List<String>? = null,
 var banner_product_tags:List<String>? = null,
 @ServerTimestamp
 var banner_timeStamp: Date? = null
)

The document itself is not getting written or uploaded in collection of the firestore database and no errors or exception also shown. when removing the @ServerTimeStamp field document is getting uploaded as the way it should

the Implementation

 override suspend fun addBannersToFirestore(banner: Banner): AddBannerResponse {
    return try {
        Log.i(TAG,"Banner Upload Started")
        val bannerId = bannerRef.document().id
        val bannerData = Banner(
            banner_id = bannerId,
            banner_title = banner.banner_title,
            banner_offer = banner.banner_offer,
            banner_image = banner.banner_image,
            banner_priority = banner.banner_priority,
            banner_tags = banner.banner_tags,
            banner_product_tags = banner.banner_product_tags
        )
        bannerRef.document(bannerId).set(bannerData).await()
        Success(true)
    } catch (e: Exception) {
        Failure(e)
    }
}

Can anyone help me to sought out the error thanks

The problem in your code lies in the fact that you're using an incorrect import:

import java.sql.Date

The right one is:

import java.util.Date

Once you change the import, your document will be added correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM