user8836786
user8836786

Reputation:

Angular / Firebase / Firestore - How to import firebase to use `serverTimestamp`

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?

Upvotes: 2

Views: 1269

Answers (1)

user8836786
user8836786

Reputation:

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

Upvotes: 5

Related Questions