0

I want to change the mail in my account, I want that in all collections where SendTO = current mail the value changed to new mail (userEmail)

I can get the collections I need, but how can I replace my old mail (SendTo field) with a new one?

// data.email new email
// userEmail currently email
await user.updateEmail(data.email);

await firestore.collection("Mission")
  .where("SendTo", "==", userEmail)
  .get()
  .then(snap => {
    let datas = snap.docs.map(doc => doc.data());
    datas.map(p => firestore.collection("Mission").doc(p.idOwner).update({
      SendTo: data.email
    }))
  });

await firebase.auth().signOut();

enter image description here

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
NoreChase
  • 55
  • 9

1 Answers1

0

With Cloud Firestore, you can't update or replace an existing element in the array. In order to achieve the update operation, you have to first remove the old element by using the arrayRemove(). And after, use the arrayUnion() operation which will add the element you wanted to put as updated.

Here is the Official Documentation for the code.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Nibrass H
  • 2,403
  • 1
  • 8
  • 14