Home > Software design >  Why item delete from mongodb database but not in UI but after reload page, item delete from UI in re
Why item delete from mongodb database but not in UI but after reload page, item delete from UI in re

Time:05-09

I try to delete an item from my UI. When I click delete button it's remove from database but not in UI. after reload my item page it's remove from UI.

CodePudding user response:

To get help on Stack Overflow you will usually need to post broken code examples.

https://www.dataschool.io/how-to-ask-for-coding-help-online/

CodePudding user response:

Though there are no broken code examples, we can probably guess what the problem is here. The most likely case is that you did not update the view after the delete happened.

Whenever you load the page, it receives data of the current state of the database at the time. If you delete something from the database in the meantime, you won't see the update in browser unless you specifically tell it to.

If you do not update your view, the browser doesn't know by itself that something should be deleted/inserted/updated.

So you just need to do something like this:

function deleteItem() {
  fetch(API_ENDPOINT, OPTIONS).then(res => updateView())

When you delete an item and receive response from the server, just delete the specific item from the DOM.

  • Related