10

I'm working on a Vue,firebase app and I'm saving the userevents in a firebase collection, I update this userevents array when a user schedule an event and want to delete it when the user cancels or remove it,

users(collection) -
 test123(document) -
      "id" . --fields
      "username" . --fields
      "userevents" . --array
         [0]
            "eventid"
            "date"
            "desc"
            "status"
         [1]
            "eventid"
            "date"
            "desc"
            "status"

From UI, if the user cancels an event, I will have the eventid and want to delete it from the userevents array. I tried removing the item from the array using below code, but it is deleting all the items from the userevents,

userRef.update({
    eventid: firebase.firestore.FieldValue.delete()
});

Is there a way to achieve this in firebase?

1 Answer 1

28

Firestore currently doesn't support deleting elements from an array using just a field from an element in that array (in your case, eventid). There are a couple of workarounds:

Method 1

You can use FieldValue.arrayRemove to remove an element from an array by value. The value has to match the whole element in the array, and it will delete all elements in the array that match that value. You can read more about that, and manipulating arrays in general here. Since your array elements are objects, the code would look like this:

userRef.update({
    "userevents": firebase.firestore.FieldValue.arrayRemove({"eventid": ..., "date": ..., "desc":..., "status":...})
});

Method 2

You're currently storing objects inside of an array. What you could do is make userevents a map (an object) instead of an array. Assuming that eventid is unique, you can index the map by eventid and then just delete the entry from the map like this:

userRef.update({
    ['userevents.' + eventid]: firebase.firestore.FieldValue.delete()
});
Sign up to request clarification or add additional context in comments.

5 Comments

i tried your method2 , but it is not deleting the key from the userevents map. I think the eventid is considered as string and not as variable as per the above syntax, userRef.update({ "userevents.${eventid}": firebase.firestore.FieldValue.delete() });
Sorry, this should work: userRef.update({ ['userevents.' + eventid]: firebase.firestore.FieldValue.delete() }); I'll update the answer as well
Another option would be to read the array from Firestore, delete the item from a certain index and then write the updated item.
Assume if the values in the array are of type double, then FieldValue.arrayRemove has to match the entire double value up to last precision, this is pain. I demand, delete by index.
2022 Cloud Update: Note that the import changed to FirebaseFirestore.FieldValue.arrayRemove({});

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.