0

Can someone tell me why this does not work:

const deleteItem = index => {
   firebase
     .firestore()
     .collection("user")
     .doc(user1)
     .update({
       Travellers : firebase.firestore.FieldValue.arrayRemove({Name: item.Travellers[index].Name, Weight: item.Travellers[index].Weight})
     })
  };

The array:

[
    {
        "id": "rWFZhAKk9eOSIIFoP0DqqvrC6WJ3",
        "Travellers": [
            {
                "Name": "Mike",
                "Weight": "100.00"
            },
            {
                "Name": "Bob",
                "Weight": "95.00"
            }
        ]
    }
]

The code works fine for arrayUnion when adding to this array, but removing it is not. If I hardcode the values into Name and Weight then it removes just fine.

Console returns (before the firebase.update) Mike and 100.00 as index 0.

Firebase screenshot:

Screenshot from Firebase

4
  • "If I hardcode the values into Name and Weight then it removes just fine." That seems key to your answer. If hardcoding the values works, the code to remove the item is fine and the problem is that your item.Travellers[index].Name are not the same as the hard-coded values. Running the code in a debugger is probably the fastest way to determine why that is. Commented Dec 14, 2021 at 15:20
  • Don't repost the same problem multiple times please. At best it'll get closed, but it may get you banned from the site. Instead, update your original post with the additional information you added here. Commented Dec 14, 2021 at 15:46
  • @FrankvanPuffelen they match up - console.log({Name: item.Travellers[index].Name, Weight: item.Travellers[index].Weight}); returns { "Name": "Mike", "Weight": "100.00" } but it still refuses to remove it Commented Dec 15, 2021 at 1:35
  • I found related post in it is explained that you cannot remove by index, because it only works with the complete value of the array. Commented Dec 28, 2021 at 10:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.