.firestore.FieldValue.html#servertimestamp
.firestore.Timestamp.html#now
I am not sure I understand the difference. Could someone explain this ?
That is: what difference would I obtain if I just used firebase.firestore.Timestamp.now() as a timestamp property of the object I am about to write instead of firebase.firestore.FieldValue.servertimestamp() ?
I would imagine there are some precision gains ? How much ? Or is it something else ?
https://firebase.google./docs/reference/js/firebase.firestore.FieldValue.html#servertimestamp
https://firebase.google./docs/reference/js/firebase.firestore.Timestamp.html#now
I am not sure I understand the difference. Could someone explain this ?
That is: what difference would I obtain if I just used firebase.firestore.Timestamp.now() as a timestamp property of the object I am about to write instead of firebase.firestore.FieldValue.servertimestamp() ?
I would imagine there are some precision gains ? How much ? Or is it something else ?
Share Improve this question asked Feb 15, 2020 at 21:47 TheProgrammerTheProgrammer 1,4994 gold badges30 silver badges54 bronze badges1 Answer
Reset to default 18Timestamp.now()
generates the timestamp using the client machine's clock, which could be wrong, even drastically wrong.
FieldValue.serverTimestamp()
encodes a token into the document field that gets translated into a timestamp using the clock on Google's servers whenever the write operation is received (not at the time it was issued from the client). The clock time on all Google services are meticulously guaranteed to be correct, no matter what a malicious user does.
If you absolutely need a correct time, especially one that needs to get checked by security rules using request.time, you should use the server timestamp. If you need the client's clock time, use Timestamp.now()
. It's almost always the case that a server timestamp is preferred.