I am trying to convert timestamp object from Firestore to Date object in TypeScript using toDate().
import { AngularFirestore } from '@angular/fire/firestore';
...
constructor(private database?: AngularFirestore) {...}
...
const someVariable = a.date.toDate();
Although the functionality is working fine in development mode but I am getting TypeScript compile check error. Also I am not able to build prod version (using ng prod) because of this error.
a.date.toDate() ~~~~ src/app/project/some.service.ts:74:36 -
error TS2339: Property 'toDate' does not exist on type 'Date'.
Any suggestions how do I resolve this?
toDate() does not work on Date, String, Number etc. Change type of your date to any for Timestamp.
date: any;
Btw, i would use toDate() usually in html files. It would show error but could compile. Type any also will clear html errors.
The error is telling you that as far as TypeScript knows, a.date is already a Date, not a Timestamp. So you need to look at the type information for whatever a is. If it's saying its date property is a Date when in fact it's a Timestamp, that's what you need to fix. (Or of course, if a.date is already a Date, you don't need that toDate call on it. But you said this code was "working in development mode" which suggests it's really a Timestamp.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With