【问题标题】:Can I get the value of serverTimestamp before saving to Firestore?我可以在保存到 Firestore 之前获取 serverTimestamp 的值吗?
【发布时间】:2018-06-05 14:54:18
【问题描述】:

我需要以 Unix Epoch 格式 12554327536 将时间戳保存到 firebase,它是自纪元以来的毫秒数。我不想使用Date().valueOf(),因为如果系统时间错误,那是不准确的。我也不能将它存储为默认的December 25, 2020 at 12:02:50 PM UTC-5,因为我需要使用firebase 的查询来按日期排序。我是这样使用的:

ref.where(`users.${userAuth.uid}`, '>', 0)
   .orderBy(`users.${userAuth.uid}`)

我在想如果我先得到值,然后转换它,然后保存它。然后我可以这样搜索和排序...或者有更好的方法吗?

【问题讨论】:

  • 你需要这样的东西吗:firebase.database.ServerValue.TIMESTAMP
  • 可能是这样的,但这会返回{.sv: "timestamp"}
  • 我刚刚检查过,它可以工作,但不要控制台尝试将其保存到您的数据库中。它是一个函数而不是一个值!在我使用的应用程序中:firebase.database().ref('users/'+this.user.uid +'/lastLogged').set(firebase.database.ServerValue.TIMESTAMP); //(new Date()).getTime()

标签: timestamp google-cloud-firestore querying


【解决方案1】:

在数据写入服务器之前,无法获取实际的服务器端时间戳。

但在提交数据之前,我们正在努力公开客户端对时间戳的估计。见https://github.com/firebase/firebase-ios-sdk/pull/537

【讨论】:

  • 我如何在安卓上做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 2017-06-24
相关资源
最近更新 更多