【问题标题】:Only Deleting item from frontend not from MongoDB仅从前端而不是从 MongoDB 中删除项目
【发布时间】: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

标签: node.js mongodb vue.js


【解决方案1】:

您正在向 post 端点 app.post('/delete/:id' 发出获取请求。您应该将 axios.get 更改为 axios.post,并且您应该等待成功确认,然后再取出前端的项目。

   {
      let uri = 'http://localhost:3000/Customer/delete/'+id;
      try{
         this.axios.post(uri).then(()=>{
            this.items.splice(id, 1);
         });
      }catch(error){
         console.log('An error occured!');
      }
   }

   //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"
        })
    }
})

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 2014-07-11
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2023-03-22
    • 2014-11-06
    相关资源
    最近更新 更多