0

Angular - v13.1 Firebase - ^9.6.2

In the old version of firebase, it was possible to import firebase into an Angular component to utilise the serverTimestamp property:

import firebase from 'firebase/app';
import 'firebase/firestore';
---
this.afs.doc(`${db_path}`).set({
 dateCreated: firebase.firestore.FieldValue.serverTimestamp(),
)};

However, since being on the new version, this approach no longer seems to work. Looking through the documentation, I haven't (...yet) found anything that suggests an alternate way to import firebase into a component to use this value.

Is there a better approach to utilise the timestamp type to set as a value?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Que
  • 957
  • 2
  • 14
  • 35

1 Answers1

4

Ok, so the following seems to work. Not sure if it's 100% correct.

Model/Interface

import { FieldValue, serverTimestamp } from "firebase/firestore";

export interface Folder extends Base {
  created: typeof serverTimestamp | FieldValue | Date;
}

Component

import { serverTimestamp } from "firebase/firestore";

---

new(){
  this.afs.collection<Model>(`path`).add({
      created: serverTimestamp()
  })
}
Que
  • 957
  • 2
  • 14
  • 35
  • Looks right, assuming this is just a client. Though your old code seems to be following the [initialization pattern for Firebase Admin](https://firebase.google.com/docs/firestore/quickstart#initialize), so perhaps that's what you're looking for? Edit: If you are using angularfire, the [quickstart](https://github.com/FirebaseExtended/angularfire/blob/master/docs/quickstart.md) shows that if you're using a webmodule (think ` – fabc Jan 21 '22 at 11:36