reactjs - axios.delete() 未运行然后阻塞
问题描述
我有一个带有 react、express 和 mongo 的小应用程序。GET 和 POST 请求在 axios 上工作得很好。DELETE 请求与后端一起工作,实际上删除了 mongo 数据库中的项目。代码片段:
deleteTodo = (event) => {
const url = "http://localhost:4000/dashboard";
const id = event.target.parentElement.getAttribute("data-id");
const deleteUrl = `${url}/${id}`;
axios.delete(deleteUrl)
.then(res => {
console.log("deleted")
});
}
但是之后什么都没有发生,我没有在控制台中收到“已删除”消息。我尝试了console.log、setState等,这些函数都没有在.then中触发。
解决方案
我在 server.js 的 delete 方法中更改了代码,现在它以某种方式工作。从:
app.delete("/dashboard/:id", (req, res) => {
const id = req.params.id;
Item.findById(id)
.then(item => item.remove())
});
至:
app.delete("/dashboard/:id", (req, res) => {
const id = req.params.id;
Item.findById(id)
.then(item => item.remove()
.then(() => res.json({ success: true })))
.catch(err => res.send("Error"))
});
推荐阅读
- hadoop - 格式化本地文件系统 HDFS 并启动 Hadoop
- delphi - TJclStringList 在 Free 上崩溃
- android - 从 iOS 和 Android 打印 html 模板
- javascript - 取消订阅以 Angular 6 停止上传进度但不是实际上传
- javascript - 在方法 Javascript 中添加 setInterval
- javascript - 反应 this.props.children.apply 不是一个函数
- python - python的lib pymysql停止随机回复
- c# - 如何将每隔一个或每个第三个字符串与通过 COM PORT 实时传入的字符串流分开?
- c# - 如何始终使用 C# StreamWriter 写入现有的本地驱动器
- apache-flink - 我正在尝试在 flink 项目中运行批处理作业,但出现此错误:无法从 JAR 文件构建程序