lewisnewson
lewisnewson

Reputation: 460

Firestore Timestamp is sometimes inaccurate

Working with Firebase at the moment and am implementing the use of the Timestamp fieldvalue for recording when documents but i'm running into a bit of a problem where the timestamp of a created document is not always entirely accurate. After some testing, it only really seems to be when the fieldvalue has been used in a short succession.

For example; If I create a new document in Firestore with the field for the first time in a few hours, the field value is accurate. However if 5 minutes later I create another document and use the fieldvalue again, it uses the timestamp from the previous made document meaning the field for both documents is the same.

I'm not sure if there is such thing as caching on this or if i'm possibly not calling the field correctly? I have this line:
export const timestamp = firebase.firestore.Timestamp.fromDate(new Date());
which I call in other files, for example adding a document to a collection:
db.collection('projects').add({ title: 'Test Project', created: timestamp })

Any insight on this would be much appreciated. Thanks!

Upvotes: 0

Views: 253

Answers (1)

l1b3rty
l1b3rty

Reputation: 3642

You keep re-using the same timestamp, so naturally it will remain the same every time.

If you want the timestamp at the (server) time of writing, use this instead:

db.collection('projects').add({ title: 'Test Project', created: firebase.firestore.FieldValue.serverTimestamp() })

Upvotes: 1

Related Questions