Get serverTimestamp from 'angularfire2/firestore'
Hi,
In the last update, angularfire2/firestore exposes the interfaces we used get from firebase package import * as firebase from 'firebase' such as QueryDocumentSnapshot and DocumentReference
I am wondering if I can also get the serverTimestamp similarly
I see the FieldValue is exported in angularfire2/firestore but it does not have serverTimestamp on it.
Is there a different way to get FieldValue.serverTimestamp() in the last update or still the old way?
Version info
Angular:
6.0.3
Firebase:
5.0.4
AngularFire:
5.0.0-rc.10
Expected behavior
import { FieldPath } from 'angularfire2/firestore';
const timestamp = FieldValue.serverTimestamp();
Actual behavior
import * as firebase from 'firebase/app';
const timestamp = firebase.firestore.FieldValue.serverTimestamp();
Hi @MurhafSousli. FieldPath is a type whereas FieldValue is an object. We recommend using the Firestore SDK for getting the server timestamp like you showed above.
in some of the docs, you guys import from firebase, rather than from firebase/app - not sure if that maters or not.
Hi @MurhafSousli Could you solve the problem? I have exactly the same
Hi guys,
import * as firebase from 'firebase/app';
const timestamp = firebase.firestore.FieldValue.serverTimestamp();
-
timestamp gives as:
[object Object]
-
If i convert these objects into strings [ JSON.stringify(timestamp ) ]
again return as:{"_methodName":"FieldValue.serverTimestamp"}
Pls tell me where i'm wrong ?
Hi @manzooralam,
it's not related to this issue but if you want to see the timestamp you should fetch the document from server, because the timestamp is computed on server side.
Then if you push item on collection as below
push(item: any): Promise<DocumentReference> {
const timestamp = this.timestamp;
return afs.collection<Item>('items');.add({
...item,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
});
}
fetch it on client (or check on firebase console) and you'll get the item.createdAt value correctly setted (timestamp)