Express.js application bug: delete post route is not found

廉价感情. 提交于 2020-03-04 18:18:13

问题


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

I have been unable to delete a post via AJAX.

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

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

In my Dashboard controller:

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>
<% } %>

Finally in public\assets\js\admin.js I have:

$(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.

UPDATE:

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.

What am I missing?


回答1:


I did it like this, for complete running example clone updated node-cheat XPressBlog and run node index followed by npm i.

added delete to routes/front-end/posts.js, like:

// Delete Single Post
router.delete('/:id', postsController.deleteSinglePost);

added deleteSinglePost to controllers/front-end/posts.js, like:

exports.deleteSinglePost = (req, res, next) => {
    const {id} = req.params;
    res.send(`This will DELETE post with id : ${id}`);
};

This is done based on looking at index.js, that is:

const dashboardRoute = require("./routes/admin/dashboard");
app.use('/dashboard', dashboardRoute);

const postsRoute = require('./routes/front-end/posts');
app.use('/', postsRoute);

curl -X DELETE http://localhost:3000/100:

This will DELETE post with id : 100




回答2:


I have solved the issue this way:

1) I replaced posts.findByIdAndRemove with Post.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) I replaced url: '/post/delete/' + postId, with url: '/dashboard/post/delete/' + postId, in my jQuery snippet.



来源:https://stackoverflow.com/questions/60490488/express-js-application-bug-delete-post-route-is-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!