How to get serverTimestamp via Angularfire or firebase
Asked Answered
T

7

9

I can't find a way to get serverTimestamp from firestore. Either with firestore itself, or with angularfire.

I found a lot of github issues with solutions, which doesn't work. I guess that's because of a different version?

"@firebase/app": "^0.1.10",
"angularfire2": "^5.0.0-rc.6.0",
"firebase": "^4.13.1",

Code alternatives I've tried:

import * as firebase from 'firebase';
firebase.firestore.FieldValue.serverTimestamp();

ERROR TypeError: Cannot read property 'FieldValue' of undefined

import * as firebase from 'firebase';
firebase.database.ServerValue.TIMESTAMP;

ERROR TypeError: Cannot read property 'ServerValue' of undefined

constructor( private afs: AngularFirestore ) { }
this.afs.firestore.FieldValue.serverTimestamp();

Property 'FieldValue' does not exist on type 'FirebaseFirestore'.

Trunkfish answered 8/5, 2018 at 13:56 Comment(1)
How does it work with firebase 9?Fullblooded
T
5

What helped at the end was uninstalling everything firebase related, so as I mentioned in my question:

"@firebase/app": "^0.1.10",
"angularfire2": "^5.0.0-rc.6.0",
"firebase": "^4.13.1",

And running npm install firebase angularfire2 --save again.

This gave me:

"angularfire2": "^5.0.0-rc.7",
"firebase": "^5.0.1",

Which fixed all my problems.

Import firebase as:

import * as firebase from 'firebase';

The timestamp code itself:

get timestamp() {
  return firebase.firestore.FieldValue.serverTimestamp();
}

EDIT on 17th november: If you'll use the previously mentioned import import * as firebase from 'firebase';, you'll get this warning in developer console:

It looks like you're using the development build of the Firebase JS SDK. When deploying Firebase apps to production, it is advisable to only import the individual SDK components you intend to use.

For the module builds, these are available in the following manner (replace with the name of a component - i.e. auth, database, etc):

CommonJS Modules: const firebase = require('firebase/app'); require('firebase/');

ES Modules: import firebase from 'firebase/app'; import 'firebase/';

Typescript: import * as firebase from 'firebase/app'; import 'firebase/';

So change it to:

import * as firebase from 'firebase/app';
import 'firebase/firestore';
Trunkfish answered 9/5, 2018 at 21:0 Comment(4)
how do you import firebase?Stanford
createdAt _methodName: "FieldValue.serverTimestamp"Orly
it created above data not time stampOrly
@Orly you maybe use JSON.stringify when pushing new item on collection, and it's why you obtain a map record _methodName: "FieldValue.serverTimestamp. If you do replace it with spread operator: afs.collection<Item>('items').add({...item)Olmos
P
9

Doing just:

import * as firebase from 'firebase';

Imports all of firebase modules which is not a good practice for production. The best practice is to use bare imports like so:

import * as firebase from 'firebase/app'; 
import 'firebase/firestore';

This will only import Firestore module in your app. You can then do:

get timestamp() {
return firebase.firestore.FieldValue.serverTimestamp();
};
Precambrian answered 7/9, 2018 at 9:54 Comment(3)
createdAt _methodName: "FieldValue.serverTimestamp"Orly
it created above data not timestampOrly
@Midhilaj, my answer is on the best way to import the firestore library to avoid importing the whole firebase in your code. Did you read through the answer before down-voting?Precambrian
H
8

Since firebase 8.0.0, there is a breaking change on how to import firebase Release Notes

Before you would import with

import * as firebase from 'firebase/app'

But now, you should import with

import { serverTimestamp } from '@angular/fire/firestore'

Use:

import firebase from 'firebase/app'
import { serverTimestamp, Timestamp } from '@angular/fire/firestore';

export type MyDoc = {
  name: string;
  createdAt: Timestamp;
};

export class MyClass {

myDoc: MyDoc = { name: 'a', createdAt: serverTimestamp() }

}

Historiographer answered 13/3, 2021 at 14:1 Comment(1)
The latest version of AngularFire (Firebase 9+, AngularFire 7+) has further breaking changes due to a redesign for reducing the bundle size. The current recommendation from the Firebase team is to use compatibility mode by changing your imports to 'firebase/compat/app' and 'firebase/compat/firestore'.Shellfish
C
5

I have this working in my project with :

import { AngularFirestore } from 'angularfire2/firestore';
import { CollectionReference, Query } from '@firebase/firestore-types'; // Not usefull for your problem
import * as firebase from 'firebase';

@Injectable()
export class MyService {

  constructor(private db: AngularFirestore) {
      console.log("firebase.firestore.FieldValue.serverTimestamp()");
      console.log(firebase.firestore.FieldValue.serverTimestamp());
  }
}

And in my packages :

"firebase": "^4.12.1",
"angularfire2": "^5.0.0-rc.5-next"

I don't have this in packages.json file:

"@firebase/app"

Conger answered 9/5, 2018 at 15:2 Comment(1)
Thank you but that didn't help me. At the end, updating the angularfire helped.Trunkfish
T
5

What helped at the end was uninstalling everything firebase related, so as I mentioned in my question:

"@firebase/app": "^0.1.10",
"angularfire2": "^5.0.0-rc.6.0",
"firebase": "^4.13.1",

And running npm install firebase angularfire2 --save again.

This gave me:

"angularfire2": "^5.0.0-rc.7",
"firebase": "^5.0.1",

Which fixed all my problems.

Import firebase as:

import * as firebase from 'firebase';

The timestamp code itself:

get timestamp() {
  return firebase.firestore.FieldValue.serverTimestamp();
}

EDIT on 17th november: If you'll use the previously mentioned import import * as firebase from 'firebase';, you'll get this warning in developer console:

It looks like you're using the development build of the Firebase JS SDK. When deploying Firebase apps to production, it is advisable to only import the individual SDK components you intend to use.

For the module builds, these are available in the following manner (replace with the name of a component - i.e. auth, database, etc):

CommonJS Modules: const firebase = require('firebase/app'); require('firebase/');

ES Modules: import firebase from 'firebase/app'; import 'firebase/';

Typescript: import * as firebase from 'firebase/app'; import 'firebase/';

So change it to:

import * as firebase from 'firebase/app';
import 'firebase/firestore';
Trunkfish answered 9/5, 2018 at 21:0 Comment(4)
how do you import firebase?Stanford
createdAt _methodName: "FieldValue.serverTimestamp"Orly
it created above data not time stampOrly
@Orly you maybe use JSON.stringify when pushing new item on collection, and it's why you obtain a map record _methodName: "FieldValue.serverTimestamp. If you do replace it with spread operator: afs.collection<Item>('items').add({...item)Olmos
I
0

I solved of this form:

importations: import { AngularFirestore } from '@angular/fire/firestore';

code:

constructor(private db: AngularFirestore) {}

private getTimestamp(): Object {
    return this.db.firestore['_firebaseApp'].firebase_.firestore.FieldValue.serverTimestamp();
}

Dependencies:

  "@angular/fire": "^5.3.0",
  "firebase": "^7.7.0",
Idio answered 31/1, 2020 at 15:43 Comment(0)
E
-1

Checkout this one

  1. Import firestore ## import { firestore } from 'firebase/app';
  2. get serverTimestamp ## createdAt = firestore.FieldValue.serverTimestamp();

Angular Firestore - serverTimestamp

Emalia answered 18/6, 2020 at 19:7 Comment(0)
P
-2
  1. import * as firebase from 'firebase/compat/app';

......

......

  1. Then try these
  • timestamp = firebase.default.firestore.FieldValue.serverTimeStamp()

or

  • timestamp = firebase.firestore.FieldValue.serverTimeStamp()
Palaestra answered 19/10, 2021 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.