I'd like to be able to automatically add server timestamps for the CRUD operations on the Firestore documents. In this article the recommendation is to use built-in serverTimestamp() function:
const timestamp = firebase.firestore.FieldValue.serverTimestamp()
ref.update({ updatedAt: timestamp })
However it appears that the nativescript plugin does not support FieldValue property. What's the recommended way to do this?
Thanks.
hi, I am needing this function too
It would be useful to know whether or not this property would be the correct one. If so, I can expose it as firebase.firestore.FieldValue.serverTimestamp():
Hi @EddyVerbruggen, that one seems to be for the Realtime database only (https://firebase.google.com/docs/reference/js/firebase.database.ServerValue).
There is a different serverTimestamp for Firestore (https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue).
I figured. Btw, you're linking to the JS SDK which I'm not using (instead, the native iOS and Android SDKs).
Oops, you are right.
By the way, I tried using something like this, for Android:
declare var com: any;
...
var timestamp = com.google.firebase.firestore.FieldValue.serverTimestamp();
...
but all I got was an empty object "{}". Was it supposed to work just like that? Or did I miss something? In any case, there seems to be a related issue: https://github.com/flutter/flutter/issues/13905#issue-286033962.
@ludcila Hmm, looks like we'll need to keep an eye on that issue (subscribe to it), thanks for investigating!
Did you get the same result?
I did not check because looking at the comments that should be the case. You can still try adding that timestamp variable to a field and see if it magically works..
I see, thanks @EddyVerbruggen!
At the very bottom of https://firebase.google.com/docs/firestore/manage-data/add-data there's an example for Android on how to do it for the update function. You can't do it because the plugin transforms your input from JS to Java, but I can update the plugin to accommodate for it.
Looks like the set function doesn't accept this type of timestamp though, so it would only work for updates (there's no example for .set in their docs either).
Gonna think about how to best approach it.
If it worked for updates it would be a sufficient workaround. Timestamps become more important in Firestore since the document ids are not created in any meaningful order so the only way to do any kind of time sorting is on the timestamp field. Thanks for looking into it.
Since ServerValue.TIMESTAMP or FieldValue.serverTimestamp() would not save to Firestore I used Date() as a workaround to set the timestamp. I used the Firestore @ServerTimestamp annotation to save the Date type field to my Firestore database.
// Get the FieldValue object
var FieldValue = require('firebase-admin').firestore.FieldValue;
// Create a document reference
var docRef = db.collection('objects').doc('some-id');
// Update the timestamp field with the value from the server
var updateTimestamp = docRef.update({
timestamp: FieldValue.serverTimestamp()
});
Funcionou perfeitamente. Link: server time stamps to specific fields in your documents to track when an update was received by the server
set() or update() can use firebase.firestore.FieldValue.serverTimestamp(), but not add()
Hi all, disregard my previous comment as we'll now need to use Timestamp on both client and servers moving forward with the latest version of _Firestore_.
With this change, timestamps stored in Cloud Firestore will be read back as com.google.cloud.Timestamp objects instead of as system java.util.Date objects. So you will also need to update code expecting a java.util.Date to instead expect a Timestamp. For example:
// Old:
java.util.Date date = (java.util.Date) snapshot.get("created_at");
// New:
Timestamp timestamp = (Timestamp) snapshot.get("created_at");
java.util.Date date = timestamp.toDate();
For my empty Object constructor in Kotlin I'm using Timestamp.now().
data class SomeObject(var id: String, var timestamp: Timestamp) {
constructor() : this("", Timestamp.now())
}
In case this helps anyone who stumbles upon this issue: serverTimestamp() resolves as null when cache data updates on a firestore listener. This is solved by using doc.data({ serverTimestamps: 'estimate' }) instead of doc.data() within query listeners.
Most helpful comment
set() or update() can use firebase.firestore.FieldValue.serverTimestamp(), but not add()