我的路线有问题,我想从包含博客和评论的页面中删除数据,
我想用这个脚本发送删除评论的请求:
trashcan.addEventListener('click', e => {
const endpoint = `/admin/blog/details/${deleteBlog.dataset.doc}`;
fetch(endpoint, {
method: 'DELETE'
})
.then(response => {
return response.json();
})
.then(data => window.location.href = data.redirect)
.catch(err => console.log(err))
})
和
trashcan.addEventListener('click', e => {
const endpoint = `/admin/blog/details/${deleteCom.dataset.doc}`;
fetch(endpoint, {
method: 'DELETE'
})
.then(response => {
return response.json();
})
.then(data => window.location.href = data.redirect)
.catch(err => console.log(err))
})
但是路由选择blogs路由, 即使我用不同的名称重命名了 url 参数 id,但 req.params 始终保持不变。
路线:
//delete blog
router.delete('/blog/details/:id', adminBlogController.blog_delete);
//delete comment
router.delete('/blog/details/:comment_id', adminBlogController.comment_delete)
我需要一种方法来传递特定 id 并在处理请求之前在路由中检查它,以便它可以选择匹配的路由。
答案 0 :(得分:0)
请使用不同的基本路由进行删除
建议*
//delete blog
router.delete('/blog/:id', adminBlogController.blog_delete);
//delete blogs's comment
router.delete('/blog/:id/:comment_id', adminBlogController.comment_delete);
或使其成为单个控制器并找到一种方法来操作控制器以区分 id 和 comment_id