[英]Firebase getting server timestamp
I am trying to structure my firebase database as such. 我正在尝试以此构建我的Firebase数据库。
year/month/date/message eg 2017/08/26/message 年/月/日期/消息,例如2017/08/26 /消息
Therefore, I need to get the firebase server time to set the new message reference path. 因此,我需要获取Firebase服务器时间来设置新的消息引用路径。 I have used
Firebase.database.ServerValue.TIMESTAMP
but it returns a placeholder that only gets converted to epoch time during inserting. 我使用了
Firebase.database.ServerValue.TIMESTAMP
但是它返回一个占位符,该占位符仅在插入期间转换为纪元时间。 Therefore my 'createdAt'
field is correct. 因此,我的
'createdAt'
字段是正确的。 But I need a way to create my desired path structure. 但是我需要一种方法来创建所需的路径结构。
let epoch = Firebase.database.ServerValue.TIMESTAMP
console.log(`epoch : ${epoch}`) // it returns an object
let date = new Date(epoch) // date creation fail here
console.log(`date : ${date}`)
let messagesRef = db.ref(`messages/${date.getFullYear()}/${date.getMonth()}/${date.getDate()}`)
let newMessage = {
createdAt: epoch
}
messagesRef.push(newMessage)
There are ways but weird, easiest way I can think of is push the message, and then grab the message to get the timestamp you just pushed. 有几种方法,但我想到的最奇怪,最简单的方法是推送消息,然后获取消息以获取刚刚推送的时间戳。
Another way is get the key of a new pushRef 另一种方法是获取新的pushRef的密钥
messagesRef.push().key
And then get timestamp from decoding the firebase key (since they are generated by timestamp) decode firebasekey 然后从解码火力键(因为它们是由时间戳生成)获得的时间戳解码firebasekey
@cartant state you can use server clock offset @cartant状态可以使用服务器时钟偏移
var offsetRef = firebase.database().ref(".info/serverTimeOffset"); offsetRef.on("value", function(snap) { var offset = snap.val(); var estimatedServerTimeMs = new Date().getTime() + offset; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.