Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'toDate' does not exist on type 'Date' Angular firestore

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?

like image 247
Aakash Goplani Avatar asked Oct 21 '25 07:10

Aakash Goplani


2 Answers

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.

like image 144
LacOniC Avatar answered Oct 23 '25 22:10

LacOniC


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.)

like image 43
T.J. Crowder Avatar answered Oct 23 '25 22:10

T.J. Crowder