简体   繁体   English

使用API​​ axios从React中立即删除mongoDB中的所有数据

[英]Delete all data in mongoDB at once from React with API axios

I'm new at programming, and need some help. 我是编程新手,需要一些帮助。 In React I'm trying to delete all items from database mongoDB with axios using map(). 在React我试图使用map()从axios删除数据库mongoDB中的所有项目。 When I click button "Delete", it removes only one first item on click. 当我单击“删除”按钮时,它仅删除单击时的第一个项目。 But I need to remove all items at once. 但我需要立即删除所有项目。

deleteAllHaspInfo = (e) => {     
     if (confirm("Do you really want to delete all hasp information from database?") === true){  //eslint-disable-line
          axios.delete("/hasp/delete", {
            data: {
              _id: this.state.hasps.map((_id) => _id)
            }
           })
           .then((res) => {
            console.log(res.data);
           })
           .catch((err) => {
            console.log(err);
         })
   } else {
     alert("Delete Canceled!");
   }       
   }
<button className="btn btn-danger" style={{margin:'0 10px'}}
          onClick={this.deleteAllHaspInfo}>Delete all hasp info from database</button> 

It needs to delete all items at once. 它需要一次删除所有项目。 I think that I'm using map() wrong... 我认为我正在使用map()错误...

Thanks, it worked for me when I used on Server query deleteMany to mongoDB: 谢谢,当我使用服务器查询deleteMany到mongoDB时,它对我有用:

 //DELETE ALL FROM DB request
app.delete("/hasp/deleteAll", function(req, res) {
    HaspInfo.deleteMany({}, function(err) {
        if (err) {
            res.status(500).send({error: "Could not clead database..."});           
        } else {
            res.status(200).send({message: "All hasp info was deleted succesfully..."});
        }
    });
});

And on React client I changed request to this: 在React客户端上,我将请求更改为:

axios.delete("/hasp/deleteAll", {data: {}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM