Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

How to set Firestore serverTimestamp on document create / update #736

Closed
KirilOkun opened this issue Jun 3, 2018 · 19 comments
Closed

How to set Firestore serverTimestamp on document create / update #736

KirilOkun opened this issue Jun 3, 2018 · 19 comments

Comments

@KirilOkun
Copy link

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.

@ludcila
Copy link

ludcila commented Jun 5, 2018

hi, I am needing this function too

@EddyVerbruggen
Copy link
Owner

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():

updateTs: firebase.ServerValue.TIMESTAMP

@ludcila
Copy link

ludcila commented Jun 5, 2018

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).

@EddyVerbruggen
Copy link
Owner

I figured. Btw, you're linking to the JS SDK which I'm not using (instead, the native iOS and Android SDKs).

@ludcila
Copy link

ludcila commented Jun 5, 2018

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: flutter/flutter#13905 (comment).

@EddyVerbruggen
Copy link
Owner

@ludcila Hmm, looks like we'll need to keep an eye on that issue (subscribe to it), thanks for investigating!

@ludcila
Copy link

ludcila commented Jun 5, 2018

Did you get the same result?

@EddyVerbruggen
Copy link
Owner

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..

@ludcila
Copy link

ludcila commented Jun 5, 2018

I see, thanks @EddyVerbruggen!

@EddyVerbruggen
Copy link
Owner

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.

@EddyVerbruggen EddyVerbruggen added this to the 6.1.0 milestone Jun 5, 2018
@KirilOkun
Copy link
Author

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.

@adam-hurwitz
Copy link

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.

@adrianojpn
Copy link

adrianojpn commented Sep 10, 2018

// 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

@tomgit2016
Copy link

set() or update() can use firebase.firestore.FieldValue.serverTimestamp(), but not add()

@adam-hurwitz
Copy link

adam-hurwitz commented Dec 9, 2018

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().

ie

data class SomeObject(var id: String, var timestamp: Timestamp) {
    constructor() : this("", Timestamp.now())
}

@nandorojo
Copy link

nandorojo commented Sep 6, 2019

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.

@kawerewagaba
Copy link

kawerewagaba commented Jul 16, 2021

So this was my scenario: I have an express cloud function that talks to the db... firebase.firestore.FieldValue.serverTimestamp() couldn't work for me for two reasons:

  1. FieldValue is not available on add() - works with set and update.

  2. The way I import and instantiate the api. Enter Timestamp.now()
    const { Firestore } = require("@google-cloud/firestore"); and const db = new Firestore();

I thought the property was available on the instance (as in db.Timestamp.now()) but it turns out the Timestamp property is actually available on the Firestore constructor.

.add({ ...req.body, created: Firestore.Timestamp.now(), })

Also, "timestamp" is not the timestamp I expected - it's more of a DATETIME object: created: July 16, 2021 at 4:37:07 PM UTC+3

Screenshot 2021-07-16 at 17 51 37

That was my experience.

@rajneshbiz
Copy link

require('firebase-admin').firestore.FieldValue.serverTimestamp() OR

require('firebase-admin').firestore.Timestamp.now()._seconds

@kawerewagaba this will work

@kawerewagaba
Copy link

kawerewagaba commented Jul 29, 2021

require('firebase-admin').firestore.FieldValue.serverTimestamp() OR

require('firebase-admin').firestore.Timestamp.now()._seconds

@kawerewagaba this will work

Thanks @rajneshbiz .

This too happened to work: Firestore.Timestamp.now().seconds

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants