简体   繁体   English

您可以在 Firebase 函数中使用 firebase-admin.firebase.Timestamp 而不是 FieldValue.serverTimestamp() 吗?

[英]Can you use firebase-admin.firebase.Timestamp instead of FieldValue.serverTimestamp() in Firebase functions?

For generating server side timestamps in Google Cloud functions it seems like the documentation tends to point to using admin.firestore.FieldValue.serverTimestamp() .为了在 Google Cloud 函数中生成服务器端时间戳,文档似乎倾向于指向使用admin.firestore.FieldValue.serverTimestamp()

But there is also an option to use import {Timestamp} from "firebase-admin/firestore" which has a function Timestamp.now() .但是也可以选择使用import {Timestamp} from "firebase-admin/firestore" ,它有一个 function Timestamp.now() Seems a much easier way to get at the current time on the server than using admin.firestore.FieldValue.serverTimestamp() .在服务器上获取当前时间似乎比使用admin.firestore.FieldValue.serverTimestamp()更容易。 Do you end up with the same time either way?无论哪种方式,您最终都会得到相同的时间吗?

Firestore timestamps are generated when the document is modified/created on the database server.当在数据库服务器上修改/创建文档时,会生成 Firestore 时间戳。 Other timestamps can be used for logic, but those are independent of Firestore timestamps.其他时间戳可用于逻辑,但它们独立于 Firestore 时间戳。

ref.set({
    createdAt: firebase.firestore.FieldValue.serverTimestamp()
})

There is no guarantee that the timestamp on the server that executes the Cloud Function will be exactly the same as the time when the data is written to the database.无法保证执行Cloud Function的服务器上的时间戳与数据写入数据库的时间完全一致。

The two operations happen on different machines, so even if the clocks were complete in sync, there's time that passes between executing the operations on those different machines.这两个操作发生在不同的机器上,因此即使时钟完全同步,在这些不同的机器上执行操作之间也会有时间间隔。 When the machines are in different zones/regions, that time difference may well be non-trivial.当机器位于不同的区域/区域时,该时间差异可能很重要。

This level of difference may of course not be important for your application, but that's something only you can determine.这种差异程度对于您的应用程序当然可能并不重要,但这只有您才能确定。 I recommend measuring the difference between the two values in your project before deciding.我建议在决定之前测量项目中两个值之间的差异。

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

相关问题 有没有办法在没有 Firebase-Admin 的情况下在 Firebase Firestore JS 中使用 FieldValue.serverTimestamp() - Is there any way to use FieldValue.serverTimestamp() in Firebase Firestore JS without Firebase-Admin firebase的云函数不能使用serverTimestamp - Cloud functions for firebase can not use serverTimestamp 在 Typescript 中使用 FieldValue.serverTimestamp() 的正确方法 - Correct way to use FieldValue.serverTimestamp() in Typescript firebase.firestore.FieldValue.serverTimestamp() 不支持 Timestamp 数据类型 - firebase.firestore.FieldValue.serverTimestamp() doesn't support Timestamp data type Swift:如何在 document().setData() Firestore 数据库中使用 FieldValue.serverTimestamp()? - Swift: How to use FieldValue.serverTimestamp() in document().setData() Firestore database? 谷歌云功能 Firebase 使用 NodeJS 更新 FieldValue.increment(1) - TypeError: FieldValue.increment is not a function - Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function 在 firebase 云功能中具有 typescript 的 firebase-admin - firebase-admin with typescript in firebase cloud functions 使用 firebase 管理员与 REST - Use firebase admin with REST firebase.firestore.FieldValue.arrayUnion 不是 function - firebase.firestore.FieldValue.arrayUnion is not a function Firebase 管理员的云功能定价 SDK - Firebase Cloud Functions pricing for Admin SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM