0

I'm trying to delete an element from an array in a Firestore document. However, none of the approaches I tried had worked so far. My last attempt was like this:

  const ref = firestore().collection('events').doc(extraid);
  ref.get().then(document => {
    const thing = document.data();
    const rejected = thing.rejected || [];
    const interested = thing.interested || [];

    const fieldIndex = interested.findIndex(obj => obj.interestedId === sender);
    const fieldToDelete = interested[fieldIndex];
    firebase.firestore.FieldValue(fieldToDelete);
    firebase.firestore.FieldValue.delete(fieldToDelete);
  });

How can I delete an element from an array in a Firestore document?

1 Answer 1

1

You will have to update() the modified array field back to the document as suggested by the documentation. Calling FieldValue.delete() isn't enough - that just creates a FieldValue token that you can pass to update() to make the change. It will look something like this:

ref.update('interested', FieldValue.delete(fieldToDelete))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.