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]);
      }
    });
  });
}

5
  • "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? Commented Jul 13, 2022 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 Commented Jul 13, 2022 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? Commented Jul 13, 2022 at 18:55
  • If the document on firestore is an array, you can try using the arrayRemove function: cloud.google.com/firestore/docs/manage-data/… Commented Jul 14, 2022 at 12:27
  • 2
    Does this answer your question? How to delete object from array in firestore Commented Jul 15, 2022 at 21:31

0

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.