angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Get serverTimestamp from 'angularfire2/firestore'

Open MurhafSousli opened this issue 8 years ago • 5 comments

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

MurhafSousli avatar Jun 06 '18 06:06 MurhafSousli

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.

davideast avatar Jun 12 '18 15:06 davideast

in some of the docs, you guys import from firebase, rather than from firebase/app - not sure if that maters or not.

robert-king avatar Sep 01 '18 01:09 robert-king

Hi @MurhafSousli Could you solve the problem? I have exactly the same

PlopezGijon avatar May 20 '19 11:05 PlopezGijon

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 ?

manzooralam avatar Jun 13 '19 06:06 manzooralam

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)

vdias38 avatar Sep 11 '19 11:09 vdias38