Javascript 使用firebase的服务器时间戳
我正在进行审查/评论过程,希望能够在firebase中存储用户为另一个用户提供的每个审查/评论,同时在相同的时间戳下提供每个审查/评级。我最初使用的是JS函数new Date().getTime(),这很好,但为了确保用户不能篡改值(即更改计算机上的日期),我想改用firebase时间戳 现在,我希望最终产品是一张地图:Javascript 使用firebase的服务器时间戳,javascript,firebase,timestamp,Javascript,Firebase,Timestamp,我正在进行审查/评论过程,希望能够在firebase中存储用户为另一个用户提供的每个审查/评论,同时在相同的时间戳下提供每个审查/评级。我最初使用的是JS函数new Date().getTime(),这很好,但为了确保用户不能篡改值(即更改计算机上的日期),我想改用firebase时间戳 现在,我希望最终产品是一张地图: [chatStartTime] : { review : stars, time : chatStartTime } 然后使用transaction.set()
[chatStartTime] : {
review : stars,
time : chatStartTime
}
然后使用transaction.set()将其合并到firebase文档中。现在的问题是ChatStartTime,它应该表示这个时间戳。我有密码:
var chatStartTime = firebase.firestore.FieldValue.serverTimestamp();
顺便说一句,我也希望得到:
var chatStartTimeDate = chatStartTime.toDate();
然而,chatStartTimeDate会导致错误——我的理解是,这是因为chatStartTime在添加到文档之前基本上是“无效”的;有没有办法绕过这个问题并使用这个功能?另外,如果我对这两个实例都使用chatStartTime,那么最终会在表单的数据库中得到一个映射:
[object Object]
review : 4.5
time :
.sv: "timestamp"
其中.sv是字符串类型。我如何解决这个问题?我希望最终结果看起来像(例如)
FieldValue.serverTimestamp()
值在写入时写入服务器。所以你不能在写之前得到它。但是,您可以从响应中获取writeTime
。这会给你你想要的。或者,您可以在编写文档后将其读回以获取时间戳(但这似乎是不必要的)
1579194722735
review : 4.5
time : 1579194722735
myCollection
.doc('foo')
.create({ timestamp: FirebaseFirestore.FieldValue.serverTimestamp() })
.then(result => {
const serverTime: Date = result.writeTime.toDate();
});