Firestore FieldValue.delete not deleting map field in transaction

Viewed 137

In my ionic 5 application, the following object is behaving differently when I updated the Firestore document directly and in transactions.

let upDoc = {
          user: firestore.FieldValue.arrayRemove(this.uid),
          ["userName." + this.uid]: firestore.FieldValue.delete()
        }

So, user is an array and userName is a map. I am trying to remove the user id from the array and remove the user name from the map.

When I do it directly like the following, both get deleted.

 this.afs.doc('user/1234/').update(upDoc);

But when I do the same in transaction, only the data from user array gets deleted but userName map data doesn't get deleted.

const doc = this.afs.firestore.doc('user/1234/');
await this.afs.firestore.runTransaction(function (transaction) {
  return transaction.get(doc).then(function (document) {
    transaction.set(doc, upDoc, {merge: true});
  })
});
1 Answers

Have you tried using update method instead of set?

await firebase.firestore().runTransaction(function (transaction) {
  return transaction.get(doc).then(function (document) {
    transaction.update(doc, upDoc);
  })
});

I was able to replicate your issue and using update method did it. I'm not totally sure about the reason but I'm looking into it.

Related