Reputation: 1451
I want to save date as firebase timestamp format. I am doing this in cloudFunctions using admin SDK.
Here is what I am trying:
deactivationDate: moment().toDate(),
deactivationDate1: new Date(),
deactivationDate2 : admin.firestore.Timestamp.fromDate(new Date()),
When I retrieve this record on client, I get :
deactivationDate: {_seconds: 1597421671, _nanoseconds: 993000000}
deactivationDate1: {_seconds: 1597421671, _nanoseconds: 993000000}
deactivationDate2: {_seconds: 1597421671, _nanoseconds: 993000000}
I am wondering why it is sending me _seconds and _nanoseconds
(with underscore)
I am not able to convert this value to date because of _ i think.
Upvotes: 0
Views: 214
Reputation: 1451
I was able to get proper timestamp value in form of Milliseconds.
The problem was I was not converting it toMillis() when retrieving from my cloud function.
I have a cloud function retrieving users like this:
admin
.firestore()
.collection("users")
.where(query)
.get();
const propertyIdUsers: FirebaseFirestore.DocumentData[] = [];
propertyIdUsersSnapshot.forEach((p) => {
const userDoc = p.data();
propertyIdUsers.push({
...userDoc,
createdAt: userDoc.createdAt?.toMillis(),
updatedAt: userDoc.updatedAt?.toMillis(),
deactivationDate: userDoc.deactivationDate?.toMillis(), // I needed to convert toMillis() right here before going to client.
// This way client receives milliseconds that will work with moment js.
});
});
Thanks to @Frank van Puffelen's comment for redirecting me to that solution.
Upvotes: 1