简体   繁体   English

将日期对象转换为Firestore时间戳

[英]Convert date object to Firestore timestamp

I'm trying to convert a date object to a Firestore timestamp. 我正在尝试将日期对象转换为Firestore时间戳。

var dateOBJ = new Date(); 
var timeStamp = new firebase.firestore.Timestamp(dateOBJ);

This gives me an error: 这给我一个错误:

Uncaught Error: Timestamp seconds out of range: Sun Dec 09 2018 11:37:05 GMT+0100

I tried converting the date object to seconds first by using .getTime() / 1000, but it's still out of range. 我尝试先使用.getTime()/ 1000将日期对象转换为秒,但仍超出范围。

The timestamp is gonna be the expiration date for an url, so I need to add some time to it. 时间戳记将是url的到期日期,因此我需要添加一些时间。

You won't get a consistent server side timestamp with a JavaScript date. 您不会获得带有JavaScript日期的一致的服务器端时间戳。 Instead, send the server timestamp from the SDK: 而是从SDK发送服务器时间戳:

const timestamp = firebase.firestore.FieldValue.serverTimestamp()

If you still want to set the timestamp as a Date you can just pass new Date() to Firestore and it will be saved as a timestamp. 如果您仍想将时间戳记设置为日期,则只需将new Date()传递给Firestore,它将被保存为时间戳记。

Frank is right about setting timestamps into firestore. 弗兰克(Frank)将时间戳设置为Firestore是正确的。

If you want to check that timestamp on the front end afterwards you need to use .toDate on the timestamp object returned from firestore to turn it back into a JS date. 如果要在之后检查前端的时间戳,则需要在从.toDate返回的时间戳对象上使用.toDate ,以将其转换为JS日期。

There are two ways of setting a date field in Cloud Firestore: 有两种方法可以在Cloud Firestore中设置日期字段:

  1. You specify a Date value for the field, in which case you fully determine what date is written. 您可以为该字段指定Date值,在这种情况下,您可以完全确定要写入的日期。
  2. You specify the firebase.firestore.FieldValue.serverTimestamp() , in which case the server writes the current date. 您指定firebase.firestore.FieldValue.serverTimestamp() ,在这种情况下,服务器将写入当前日期。

There is no way in the API to combine these two options, you either use one or the other. API中没有办法将这两个选项组合在一起,您只能使用其中一个。

Since you comment that you want to store the timestamp and an offset, that is also what I'd store: 由于您评论您要存储时间戳和偏移量,因此这也是我要存储的内容:

  • a timestamp field that you let the server populate with firebase.firestore.FieldValue.serverTimestamp() . 一个让服务器使用firebase.firestore.FieldValue.serverTimestamp()填充的timestamp字段。
  • a offset field, that you populate from the app with the offset in days/hours. offset字段,您可以使用天/小时为单位从偏移量中填充应用程序。

That way you can reconstitute the effective expiration timestamp by combining the two fields. 这样,您可以通过组合两个字段来重新构造有效的到期时间戳。


You could even add a third field that stores the expiration timestamp, but that will require an extra write operation. 您甚至可以添加第三个字段来存储到期时间戳,但这将需要额外的写操作。 I'd typically do this in Cloud Functions, to ensure you keep full control over the field and clients can't spoof it. 我通常会在Cloud Functions中执行此操作,以确保您完全控制该字段,并且客户不会对其进行欺骗。 If you don't do it in Cloud Functions, consider writing security rules that validate that the value if the calculated field is indeed the result of that calculation. 如果您没有在Cloud Functions中执行此操作,请考虑编写安全规则以验证该值(如果计算出的字段确实是该计算的结果)。

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

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