0

I'm writing a new document to Firestore. One of the fields must have some text, followed by the date, formatted like so 10/11/2020, 13:07. What would be the simplest way to achieve this? I have an exceptionally convoluted way of achieving this with Moment.js in an old project. But I would like to see if there's a more elegant approach.

this.doc.set({
  title: "A New Title " + ,
  date: firebase.firestore.FieldValue.serverTimestamp(),
})

In the above snippet, the title would need to be something like title: "My New Title " + date" so that the final result is something like: My New Title - 10/11/2020, 13:07

1
  • Apologies Frank. I was away for a few days. Updated. I'll respond to your post too. Commented Nov 13, 2020 at 9:28

1 Answer 1

0

There is no way to merge a server-side timestamp with another value in a single write.

You will either have to:

  1. Use a client-side date/time in the title.
  2. Use an extra operation to read back the server-side data, and add it to the title.
  3. Keep the date as a separate field (like you have now), and add it to the title whenever read read the document.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Frank. I appreciate the time. I just need to find an elegant way to achieve that.

Your Answer

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