0

When deleting a bank from the database, I need to redraw the screen and delete the card with the bank that I deleted. I wrote a filter function that returns elements that are not equal to the id of the element that was removed, but it does not work. How can it be solved?

function getBanks() {
  db.collection("banks").onSnapshot((snapshot) => {
    let banks = [];
    snapshot.docChanges().forEach((change) => {
      const bank = {
        id: change.doc.id,
        ...change.doc.data(),
      };
      if (change.type === "added") {
        banks.push(bank);
        generateBanks([bank]);
      } else if (change.type === "removed") {
        banks.filter((item) => item.id != bank.id);
        generateBanks([bank]);
      }
    });
  });
}
userName
  • 903
  • 2
  • 20
  • "it does not work" is really hard to help with. When you step through the code you shared in a debugger, which specific line doesn't do what you expect it to do? – Frank van Puffelen Jul 13 '22 at 17:56
  • When I delete the bank, nothing happens. The element is not removed from the array, but it is removed from the database – userName Jul 13 '22 at 18:17
  • So if you set a breakpoint on the `snapshot.docChanges()` line in your code, run in a debugger, and delete a bank: does it trigger that breakpoint? If so, if you step through the code line by line from there, what `docChanges` do you get for the delete operation? – Frank van Puffelen Jul 13 '22 at 18:55
  • If the document on firestore is an array, you can try using the `arrayRemove` function: https://cloud.google.com/firestore/docs/manage-data/add-data#update_elements_in_an_array – Nicola Spadari Jul 14 '22 at 12:27
  • 2
    Does this answer your question? [How to delete object from array in firestore](https://stackoverflow.com/questions/52150386/how-to-delete-object-from-array-in-firestore) – Rogelio Monter Jul 15 '22 at 21:31

0 Answers0