简体   繁体   English

如何在 Firestore 文档 android 中添加服务器时间戳

[英]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.当在数据 class 中添加字段值足以在 firestore 文档中创建时间戳时。 but in my case i did the same但就我而言,我做了同样的事情

data class数据 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.文档本身没有写入或上传到 firestore 数据库的集合中,也没有显示错误或异常。 when removing the @ServerTimeStamp field document is getting uploaded as the way it should删除 @ServerTimeStamp 字段文档时,它应该按应有的方式上传

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.更改导入后,您的文档将被正确添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 android 设备中的 Firestore 获取服务器时间戳? - How to get server Timestamp from Firestore in an android device? 在 Firestore 文档中添加时间戳 - Add timestamp in Firestore documents 如何在 filds firestore 中添加文档的 id - how to add id of document in filds firestore Flutter Firestore:将数据列表添加到 Firestore 文档 - Flutter Firestore : Add a List of Data to Firestore Document 如何在一次又一次地点击提交按钮后通过生成不同的文档 ID 将数据添加到 firebase 火库? Android Kotlin - How to add data to the firebase firestore by generating different document id after hitting submit button again and again? Android Kotlin 如何在 Firestore 文档的 map 中添加或编辑特定值? - How to add or edit specific value within a map on a firestore document? 如何检查在 Java 中的 Firestore 上删除了哪个文档 - Android Studio - How to check which document is deleted on Firestore in Java - Android Studio 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的文档及其子集合? - How to display a document with its subcollections from Firestore in a RecyclerView with Android? 如何读取 Swift 中的 Firestore 时间戳 - How to read a Firestore timestamp in Swift 如何从 Firestore 检索文档并将其分配给变量? [安卓] [科特林] - How to retrieve a document from Firestore and assign it to a variable? [ANDROID] [KOTLIN]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM