Reputation: 989
I have a custom object called myData
, and within that object I have a timestamp. Considering that the Node driver for Firebase doesn't support custom Objects, I'm quickly converting the data to pure JSON using the following line:
const dataAsJson = JSON.parse(JSON.stringify(myData));
However, when it does this it strips out the Date from the object and turns it into a string (if it is initially of type Date
), or a map if it is of type FirebaseFirestore.Timestamp
.
I suppose I could shoot another direct set on the same object right after with const res = await docRef.set(data, {merge: true});
if data = {dateExample: admin.firestore.Timestamp.fromDate(new Date('December 10, 1815'))}
but I'd rather do it all in one go.
Has anyone solved this before? I'm fear if I mess with a batch commit I may not be able to choose which gets set first.
Upvotes: 0
Views: 1085
Reputation: 317322
Stringifying then parsing your data will obviously destroy types of dates and timestamps in the object. Those need to remain intact in order to be recognized by Firestore.
You will have to provide a plain JavaScript object representation of the object you want to store. This could be as simple as just copying the properties into a new object based on the original object. You will need to provide JavaScript Date or Firestore Timestamp objects in order to get a proper timestamp type in the document. Same for reference types.
Since we can't see the object you're trying to serialize, I can give no specific advice, but it should be clear by now what you need to do.
Upvotes: 1