Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

node.js - Trying to activate axios.delete method from react to node JS via title name from input

I'm trying to delete a listing from my mongoDB collection via mongoose react and axios , i've managed to make it work one time but somehow i messed it up and couldent figure out why , thanks for the help!

React Function

const handleDeleteProperty = async () => {

 let title = document.getElementById('title').value

  try{
      const res = await axios.delete('/delete', title);
      if(res.data.success){
          alert(res.data.msg);
      }
  }
  catch(err){
      console.error(err);
  }
}
 

React function evoking

  <button onClick={handleDeleteProperty}>DELETE LISTING</button>

Backend(List is my mongoose schema)

app.get("/delete", (req, res) => {
 
});

app.delete('/delete', async (req, res) => {
  
  try{
      await List.findOneAndRemove(req.body.title);
      return res.status(200).json({ success: true, msg: 'Product Deleted' });
  }
  catch(err){
      console.error(err);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...