After extensive searching, I am still unable to figure out how to handle redirection after a DELETE request. Below is the code snippet I am currently using WITHOUT THE REDIRECT:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
});
};
The remove
function triggers when an item (post) is being deleted. While it functions as expected (although requiring res.send(200)
for proper operation), I am struggling with implementing the redirect feature. When attempting to insert res.redirect('/forum')
within the remove
function like so:
exports.remove = function(req, res) {
var postId = req.params.id;
Post.remove({ _id: postId }, function(err) {
if (!err) {
console.log('notification!');
res.send(200);
}
else {
console.log('error in the remove function');
res.send(400);
}
res.redirect('/forum');
});
};
The system interprets the redirect as a DELETE request trying to erase /forum
, resulting in:
DELETE http://localhost:9000/forum 404 Not Found 4ms
All I require is a page refresh to update the post list following deletion. Any assistance would be greatly appreciated.