I want to delete data from UI and Server, I write this code, but the problem is when I click the
Deletebutton data was deleted from UI but still exists in the server. After reloading the page deleted data showed in the UI again. when Iconsole.log(data)the result is{ acknowledged: true, deletedCount: 0 }.
/* server side code */
app.delete('/data/:id', async (req, res) => {
const id = req.params.id
const query = { _id: ObjectId(id) }
const result = await dataCollection.deleteOne(query)
res.send(result)
})
/* client side code */
const [data, setData] = useData()
const deleteData = async (id) => {
const confirmation = window.confirm('Are you sure about delete the data ?')
if (confirmation) {
const { data } = await axios.delete(`http://localhost:5000/data/${id}`, product)
const exist = data.filter( x => x._id !== id);
setData(exist)
})
}
The result
{ acknowledged: true, deletedCount: 0 }
Expected result{ acknowledged: true, deletedCount: 1 }