Greg Fenton
Greg Fenton

Reputation: 2808

Adding a Timestamp to a nested object in Cloud Firestore

I am trying to set a timestamp field on a nested element to the current time:

docRef.update({
   arrayOfStuff: {
        id: 123,
        dateAdded: admin.firestore.FieldValue.serverTimestamp()
   }
})

I get the error:

FieldValue.serverTimestamp() cannot be used inside of an array

Using Date.now() gets me an int value, not a timestamp.

Trying Firestore.Timestamp.fromDate() yields the error:

TypeError: date.getTime is not a function
    at Function.fromDate (/srv/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:42)
    at firestoreDB.doc.get.then.site (/srv/index.js:32:45)

where line 32 of index.js is:

let now = admin.firestore.Timestamp.fromDate(Date.now());

Thoughts?

Upvotes: 1

Views: 982

Answers (1)

tzovourn
tzovourn

Reputation: 1321

As explained here, "FieldValue.serverTimestamp can't be supported inside arrays without a major overhaul of the way Firestore works". So this behavior is expected.

Searching a bit more, I was able to find a workaround by Renaud Tarnec. Although he mentioned that you might need to change your data model.

I hope this helps.

Upvotes: 1

Related Questions