Express.js应用程序错误:找不到删除发布路由 [英] Express.js application bug: delete post route is not found

查看:95
本文介绍了Express.js应用程序错误:找不到删除发布路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 博客应用程序 (点击指向请参见 GitHub 回购)和 Express

I am working on a blogging application (click the link to see the GitHub repo) with Express, EJS and MongoDB.

我无法通过AJAX删除帖子.

I have been unable to delete a post via AJAX.

在我的仪表板路由文件(routes\admin\dashboard.js)中,我具有:

In my Dashboard routes file (routes\admin\dashboard.js) I have:

// Delete Post
router.delete('/post/delete/:id', dashboardController.deletePost);

在我的仪表板控制器中:

exports.deletePost = (req, res, next) => {
    const postId = req.params.id;
    posts.findByIdAndRemove(postId, function(err){
        if (err) {
            console.log('Error: ', err);
        }
        res.sendStatus(200);
    });
}

在列出表中帖子的视图中,对于每个人都有的编辑"和删除"按钮:

In the view that lists the post in a table, with and "Edit" and a "Delete" button for each one I have:

<% if (posts) { %>
    <% posts.forEach(function(post) { %>
        <tr data-id="<%= post._id %>" class="d-flex">
            <td class="col-1"></td>
            <td class="col-4 col-lg-5">
                <%= post.title %>
            </td>
            <td class="col-2"></td>
            <td class="col-2"></td>
            <td class="col-3 col-lg-2 text-right">
                <div class="btn-group">
                    <a href="#" class="btn btn-sm btn-success">Edit</a>
                    <a href="#" class="btn btn-sm btn-danger delete-post" data-id="<%= post._id %>">Delete</a>
                </div>
            </td>
        </tr>
        <% }); %>
<% } else { %>
        <tr>
           <td colspan="5">There are no posts</td>
        </tr>
<% } %>

最后在public\assets\js\admin.js中,我有

$(document).ready(function(){
  $('.delete-post').on('click', function(evt){
    evt.preventDefault();
    let postId = $(this).data('id');
    if(confirm('Delete this post?')) {
      $.ajax({
        url: '/post/delete/' + postId,
        method: 'GET',
        success: function(deleteMsg){
          $('tr[data-id="' + postId +'"]').fadeOut('250');
        }
      });
    }
  });
});

我已经足够成功完成删除操作,但事实并非如此.

I have that was enough for the delete operation to be successful, but it is not.

更新:

如果将url: '/post/delete/' + postId,替换为url: '/dashboard/post/delete/' + postId,,则会得到500 internal server error.我在网络"标签中看到了posts is not defined.

If I replace url: '/post/delete/' + postId, with url: '/dashboard/post/delete/' + postId, I get a 500 internal server error. I see posts is not defined in the network tab.

我想念什么?

推荐答案

我已经通过以下方式解决了该问题:

I have solved the issue this way:

1)我用Post.findByIdAndRemove替换了posts.findByIdAndRemove:

exports.deletePost = (req, res, next) => {
    const postId = req.params.id;
    Post.findByIdAndRemove(postId, function(err){
        if (err) {
            console.log('Error: ', err);
        }
        res.sendStatus(200);
    });
}

2)我在jQuery代码段中将url: '/post/delete/' + postId,替换为url: '/dashboard/post/delete/' + postId,.

2) I replaced url: '/post/delete/' + postId, with url: '/dashboard/post/delete/' + postId, in my jQuery snippet.

这篇关于Express.js应用程序错误:找不到删除发布路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆