【发布时间】:2021-02-16 14:26:16
【问题描述】:
我的路线有问题,我想从包含博客和 cmets 的页面中删除数据,
我想使用此脚本发送删除 cmets 的请求:
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))
})
但是路由选择博客路由, 即使我用不同的名称重命名了 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 并在处理请求之前在路由中检查它,以便它可以选择匹配的路由。
【问题讨论】:
-
那些是相同的路由,遇到的第一个路由会匹配。您需要一些方法来区分路线。将修改不同底层资源的路由命名为同一件事是违反直觉的。一个修改 cmets,一个修改 blog。您的客户端代码应该是出现问题的重要线索——除了参数值之外,URL 是相同的。
-
是的,即使我更改了参数的名称,req.params 的值也相同,我尝试使用不同的路由,它对我有用,非常感谢您的回复和所有这些信息
标签: javascript node.js express routes