无法从数据库中删除特定 post
Unable to delete a specific post from database
Thee app.get rout 是显示 post 内容,我有一个删除按钮,我想从中删除特定的 post 并渲染返回主路线但无法执行该任务请帮助
//This is the app.js code in which I think the error sustain in delete part
app.get("/posts/:postId", function (req, res) {
let requestedPostId = req.params.postId;
Post.findOne({ _id: requestedPostId }, function (err, post) {
res.render("post", {
title: post.title,
content: post.content,
});
});
});
app.delete("/posts/:postId", function (req, res) {
Post.deleteOne({ _id: req.params.postId }, function (err) {
if (!err) {
res.send("SuccesFully Deleted this Post");
} else {
res.send(err);
}
});
});
// This the the ejs file containing the delete button
<form class="delete" action="/posts/:postId" method="delete">
<button class="btn btn-danger delete-btn" type="submit">
<a class="delete-btn-a" href="/">Delete this Blog</a></button>
</form>
我使用了 post 路由,在该路由下我使用了 MongoDB
的 deleteOne 属性
app.post("/posts/:postId/delete", function (req, res) {
Post.deleteOne({ _id: req.params.postId }, function (err) {
if (err) {
res.send(err);
} else {
console.log("SuccesFully Deleted this Post");
res.redirect("/");
}
});
});
问题出在我的 ejs 文件中,在该文件中我没有使用 ejs 语法来获取 post 操作 ID
<form class="delete" action="/posts/<%=postId%>/delete" method="POST">
<button class="btn btn-danger delete-btn" type="submit" title="Delete this blog">
<a class="btn-a"><i class="bi bi-trash"></i></a>
</button>
</form>
我希望阅读这篇文章的人post能够理解如果您有任何问题可以发表评论并提出问题,我很乐意解决问题
Thee app.get rout 是显示 post 内容,我有一个删除按钮,我想从中删除特定的 post 并渲染返回主路线但无法执行该任务请帮助
//This is the app.js code in which I think the error sustain in delete part
app.get("/posts/:postId", function (req, res) {
let requestedPostId = req.params.postId;
Post.findOne({ _id: requestedPostId }, function (err, post) {
res.render("post", {
title: post.title,
content: post.content,
});
});
});
app.delete("/posts/:postId", function (req, res) {
Post.deleteOne({ _id: req.params.postId }, function (err) {
if (!err) {
res.send("SuccesFully Deleted this Post");
} else {
res.send(err);
}
});
});
// This the the ejs file containing the delete button
<form class="delete" action="/posts/:postId" method="delete">
<button class="btn btn-danger delete-btn" type="submit">
<a class="delete-btn-a" href="/">Delete this Blog</a></button>
</form>
我使用了 post 路由,在该路由下我使用了 MongoDB
的 deleteOne 属性app.post("/posts/:postId/delete", function (req, res) {
Post.deleteOne({ _id: req.params.postId }, function (err) {
if (err) {
res.send(err);
} else {
console.log("SuccesFully Deleted this Post");
res.redirect("/");
}
});
});
问题出在我的 ejs 文件中,在该文件中我没有使用 ejs 语法来获取 post 操作 ID
<form class="delete" action="/posts/<%=postId%>/delete" method="POST">
<button class="btn btn-danger delete-btn" type="submit" title="Delete this blog">
<a class="btn-a"><i class="bi bi-trash"></i></a>
</button>
</form>
我希望阅读这篇文章的人post能够理解如果您有任何问题可以发表评论并提出问题,我很乐意解决问题