'I wanna delete a post from my UI but I have a problem
I want to delete a post from my UI. I already did app.delete in my server site. I use MongoDB and nodejs for the server.
const ManageItem = () => {
const navigate = useNavigate();
const [items, setItems] = useItems();
const handleToNavigate = () => {
navigate('/additem')
}
const handleDelete = id => {
const proceed = window.confirm('Are you sure you want to delete?');
if (proceed) {
const url = `http://localhost:5000/item/${id}`;
fetch(url, {
method: 'DELETE'
})
.then(res => res.json())
.then(data => {
console.log(data);
const remaining = items.filter(item => item._id !== id);
setItems(remaining);
})
}
Solution 1:[1]
Your question is very unclear. For now I can only assume that you are filtering out the deleted data and setting the rest of the data in a state. Either you are not loading the state in the ui properly or there is something wrong in your server side. Please provide your code for better understanding. Till then try this out in your server side, if you haven't already.
app.delete("/item/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await databaseCollection.deleteOne(query);
res.send(result);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rittika Dev |
