I am working on an interface that utilizes Firestore timestamps for date settings.
export interface Album{
album_name: string,
album_date: firebase.firestore.FieldValue;
}
Adding a new item functions perfectly:
this.albumsCollection.add({
album_name: "My Album",
album_date: firebase.firestore.FieldValue.serverTimestamp(),
})
Retrieving the album document from firestore
When retrieving an album Document from firestore, excluding the album_date
works fine. However, I aim to display it in the Angular Material Date Picker using toDate
.
public getAlbum(): Observable<Album> {
this.itemDoc = this.afs.doc<Album>(`albums/1`);
return this.itemDoc.snapshotChanges()
.pipe(map(collectionDoc => {
const data = collectionDoc.payload.data();
return {
...data,
album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
} as Inspection;
}),
shareReplay()
);
}
The error message received on the line:
album_date: (data.album_date as firebase.firestore.Timestamp).toDate(),
is as follows:
Conversion of type '{ album_Date: Date; }' to type ‘Album` may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. Types of property ‘album’_date are incompatible. Property 'isEqual' is missing in type 'Date' but required in type 'FieldValue'.
What would be the most effective approach to utilize 'toDate' on a returned firestore timestamp while also having the interface configured for firestore?