0

The current angularfire docs (Angularfire 6.0.4, Latest commit d91965e on Mar 30 2020), says only this on manipulating documents:

AngularFirestore provides methods for setting, updating, and deleting document data.

set(data: T) - Destructively updates a document's data.

update(data: T) - Non-destructively updates a document's data.

delete() - Deletes an entire document. Does not delete any nested collections.

When interacting directly with Firebase (i.e. not using angularfirestore), firebase is pretty clear that an update will fail if the document does not exist; one must use the set operation for new documents (perhaps with the {merge: true} option as a safety catch in case the document exists).

What does the Angular Firestore update(data: T) command do if the document does not exist?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
glenn
  • 271
  • 2
  • 6

1 Answers1

0

update() will fail of the document doesn't already exist.

If you need to create-or-update in one operation that does not fail whether or not the document already exists, you should use set() and pass { merge: true } to it as the second argument. Or you can use a transaction to make an atomic get-and-set operation depending on what's returned by get().

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks, Doug. However, your link goes to a comment on Firestore. I'm asking specifically about AngularFire, version 6 (my question notes the Firestore behavior). It is very possible that the AngularFire update wraps the low-level Firestore method. Can you confirm that your answer applies to AngularFire 6.x? – glenn Nov 03 '20 at 14:07
  • Angularfire is just a wrapper around the core JavaScript SDK. They have similar APIs. – Doug Stevenson Nov 03 '20 at 16:51