1

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?

1
  • So I have decided to move forward using: let now = admin.firestore.Timestamp.now() It yields a "timestamp" value in Firestore. I suspect the difference is that the timestamp I get comes from when the code runs, as opposed to serverTimestamp() that I believe is the time that the data is actually stored in Firestore (there can be a delay between the two times). Commented Apr 4, 2020 at 3:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

See here a possible workaround with a Cloud Function and a field in the doc (not in an array) that contains the timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.