Search code examples
firebasegoogle-cloud-firestoretransactions

Get the updated document into a Firebase Firestore transaction


I'm trying to keep updated a local copy for a document from Firestore. This means, each time I update that document, I'm trying to keep my local copy in sync with the document updated. The problem here is that I'm using a serverTimestamp value. So I can have all my document properties updated but I don't have any way to convert this serverTimestamp to a local Date.

I'm doing this stuff into a Firestore transaction. For that reason I'm looking to have the updated document as the result of that operation. But seems that Firestore has this limitation itself. The most "reasonable" approach is get the document again, but Firestore has the limitation about all the read must be before all the writes into a transaction.

Could be great if firestore gets a WriteResult as in a not-transaction situation. (ref: https://googleapis.dev/nodejs/firestore/latest/WriteResult.html)

Anybody have idea about how can I deal with this problematic?

Thanks in the updated


Solution

  • You'll have to read the document again after the transaction completes. You won't be able to do it inside the transaction callback block, either by get() or by a listener that you set up ahead of time (that would trigger after the transaction is done). The transaction does not deliver you the final state of the written document.