【发布时间】:2020-04-28 17:09:31
【问题描述】:
我正在制作一个简单的 CRUD 应用程序,使用 Vue.js 作为前端 Nodejs 作为后端,MongoDB 作为数据库。我有一个删除功能,但问题是它只从前端而不是从数据库中删除客户,当我刷新页面以查看删除后的所有客户时,客户再次出现在前端.. 下面是我的删除功能
{
let uri = 'http://localhost:3000/Customer/delete/'+id;
this.items.splice(id, 1);
this.axios.get(uri);
}```
//AND HERE IS MY BACKEND API FOR DELETE Customer
app.post('/delete/:id', async (req, res) => {
try {
Customer.findByIdAndRemove(req.params.id, req.body, function (err, doc) {
if (err) res.json(err);
else res.json('Successfully removed');
})
} catch (error) {
res.json({
response: "Invalid ID"
})
}
});```
【问题讨论】:
-
您正在向 post 端点
app.post('/delete/:id'发出获取请求。您应该将 axios.get 更改为 axios.post