I store a list of users invited to an organisation, together with the timestamp denoting when the invite expires in Firebase. When I pull the timestamp from Firebase, I parse it to determine if it's still valid or has expired using:
Client Code
const inviteStatus = invitee?.validUntil.toDate() >= Date.now() ? 'valid_invitee' : 'expired_invitee'
I have also built in functionality to generate new invites via a call to Firebase Cloud Functions. Once an invite has been created, I return a new invitee object:
Firebase Functions Code
const expiryDate = admin.firestore.Timestamp.now().toDate()
expiryDate.setTime(expiryDate.getTime() + (1000 * 60 * 60 * 24 * 7)) // 7-day expiration
return {
invitee: {
validUntil: admin.firestore.Timestamp.fromDate(expiryDate)
}
}
The reason I don't return just a Date object but instead convert it to Firestore's Timestamp format is to ensure that client always receives the validUntil data in the same format, regardless of whether it's read directly from Firestore DB or returned via Firebase Cloud Functions.
However, once this invitee object is returned and the client code runs (see first code snippet), I get the following error:
Uncaught TypeError: invitee.validUntil.toDate is not a function
Investigating
I have printed both objects (one directly pulled from DB and another returned via Cloud Functions) and noticed that the object returned directly from DB, when printed, shows a ut in Chrome Dev Tools next to the validUntil object, whereas the object returned from Cloud Functions does not display ut.
Printed Object Queried from Firestore

Printed Object Returned from Cloud Functions

Question
Why is Firebase generating two different types of timestamp objects where one can be converted to Date using .toDate() function and another throws an error?
Is there a way to return a proper timestamp object via Cloud Functions that I will be able to apply .toDate() function on in the client?