Angularfire: saved "date" returns timestemp..?

Created on 29 May 2018  路  6Comments  路  Source: angular/angularfire

I just made an update after which I started to get error with all the "date" objects retrived from firestore, ex:
I have this class:
class Shift{ statDate?:Date; endDate?:Date; employeeId?:string; }
so that later I can just use it in my html like so
{{shift.startDate|date:"yyyy MMM dd":"":""}}
but now the value of startDate is now retrived as a timestemp
made a fix this.firestore.firestore.settings({ timestampsInSnapshots: false });
but now I'm getting message

@firebase/firestore: Firestore (4.13.0):
The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK

do I realy need to change logic everywhere in my application, I have object with "Date"s all over the place:S...?

Most helpful comment

I think it should be noted, in the documentation also, that the switch to timestamp will be a permanent one, and the use of Date objects in model interfaces/classes is discouraged. While you convert your models and write bunch of angular pipes etc. to directly deal with Timestamp, you CAN restore the old functionality by adding this provider in your app.module.ts file:

import { AngularFirestoreModule, FirestoreSettingsToken } from 'angularfire2/firestore'
// later in file
{ provide: FirestoreSettingsToken, useValue: { timestampsInSnapshots: false } },

Where the value for useValue conforms to firebase.firestore.Settings from the main firebase.js. The story does not yet tell how long this setting will be available...

All 6 comments

You can change it after your */snapshot/valueChanges with a .map pretty easily

How do you convert the returned Timestamp back to a Date object (without moment)?

@jamesdaniels well I did it like in the code bellow but it looks preaty ugly -is there better solution?:
snapshotChanges().pipe(map(changes => { return changes.map(a => { const shift = a.payload.doc.data() as Shift; const timeStemp: any = shift.startDate; shift.startDate = (timeStemp as Timestamp).toDate(); return shift; });

I was getting dates in the 70's because I removed too many zeros. But if you add the seconds and nanoseconds / 1000 and create a Date from that it looks ok

yeap U can do it like this as well but U still can not do something like this:
shift.startDate = new Date((shift.startDate as Timestemp).seconds*1000) beacuse like in my case 'startDate' is an Date Object and can not be cast to "TimeStemp" direcly. So I would have to do something like I did in my answer before or I would have to change Date object in all my classes to "any" -which I don't think that @jamesdaniels had in mind when he wrote that it can be easily convert in 'map':S

I think it should be noted, in the documentation also, that the switch to timestamp will be a permanent one, and the use of Date objects in model interfaces/classes is discouraged. While you convert your models and write bunch of angular pipes etc. to directly deal with Timestamp, you CAN restore the old functionality by adding this provider in your app.module.ts file:

import { AngularFirestoreModule, FirestoreSettingsToken } from 'angularfire2/firestore'
// later in file
{ provide: FirestoreSettingsToken, useValue: { timestampsInSnapshots: false } },

Where the value for useValue conforms to firebase.firestore.Settings from the main firebase.js. The story does not yet tell how long this setting will be available...

Was this page helpful?
0 / 5 - 0 ratings