如何使用 Axios 删除数据库条目?
How do I delete a database entry using Axios?
我正在尝试使用 Axios 从我的数据库中删除评论并收到 404 错误。创建、读取和更新都正常工作。我假设这可能是因为我在删除请求中的 url 与数据库中列出的 ID 不匹配?
App.js 路由器:
<Router>
<Route exact path="/" component={Reviews}/>
<Route path="/edit/:id" component={Edit}/>
<Route path="/create" component={Create}/>
<Route path="/delete/:id" component={Delete}/>
<Route path="/about" component={About}/>
<Route path="/contact" component={Contact}/>
</Router>
</Container>
</>
)
}
}
删除组件:
export default class Delete extends Component {
constructor(props) {
super(props);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
title: '',
releaseDate: 0,
rating: 0,
artist: '',
review: ''
}
}
onSubmit(e) {
e.preventDefault();
console.log(this.props.match.params.id)
axios.delete('http://localhost:4000/reviews/delete/'+this.props.match.params.id)
.then(res => console.log(res.data));
this.props.history.push('/')
}
render() {
return (
<>
<Jumbotron className='text-center'>
<h2>Delete</h2>
</Jumbotron>
<div>
<h3>Delete "{this.state.title}" ?</h3>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<input type="submit" value="Delete Review" className="btn btn-primary" />
</div>
</form>
</div>
</>
)
}
}
服务器:
reviewRoutes.route('/delete/:id').post(function(req,res) {
Review.findByIdAndRemove(req.params.id, (err, review) => {
if (err) return res.status(500).send(err);
const response = {
message: "Review successfully deleted",
id: review._id
};
return res.status(200).send(response);
});
});
错误:
reviewRoutes.route('/delete/:id').delete(function(req,res) {
Review.findByIdAndRemove(req.params.id, (err, review) => {
if (err) return res.status(500).send(err);
const response = {
message: "Review successfully deleted",
id: review._id
};
return res.status(200).send(response);
});
});
您正在发出 post 请求而不是删除。
我正在尝试使用 Axios 从我的数据库中删除评论并收到 404 错误。创建、读取和更新都正常工作。我假设这可能是因为我在删除请求中的 url 与数据库中列出的 ID 不匹配?
App.js 路由器:
<Router>
<Route exact path="/" component={Reviews}/>
<Route path="/edit/:id" component={Edit}/>
<Route path="/create" component={Create}/>
<Route path="/delete/:id" component={Delete}/>
<Route path="/about" component={About}/>
<Route path="/contact" component={Contact}/>
</Router>
</Container>
</>
)
}
}
删除组件:
export default class Delete extends Component {
constructor(props) {
super(props);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
title: '',
releaseDate: 0,
rating: 0,
artist: '',
review: ''
}
}
onSubmit(e) {
e.preventDefault();
console.log(this.props.match.params.id)
axios.delete('http://localhost:4000/reviews/delete/'+this.props.match.params.id)
.then(res => console.log(res.data));
this.props.history.push('/')
}
render() {
return (
<>
<Jumbotron className='text-center'>
<h2>Delete</h2>
</Jumbotron>
<div>
<h3>Delete "{this.state.title}" ?</h3>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<input type="submit" value="Delete Review" className="btn btn-primary" />
</div>
</form>
</div>
</>
)
}
}
服务器:
reviewRoutes.route('/delete/:id').post(function(req,res) {
Review.findByIdAndRemove(req.params.id, (err, review) => {
if (err) return res.status(500).send(err);
const response = {
message: "Review successfully deleted",
id: review._id
};
return res.status(200).send(response);
});
});
错误:
reviewRoutes.route('/delete/:id').delete(function(req,res) {
Review.findByIdAndRemove(req.params.id, (err, review) => {
if (err) return res.status(500).send(err);
const response = {
message: "Review successfully deleted",
id: review._id
};
return res.status(200).send(response);
});
});
您正在发出 post 请求而不是删除。