I have a specific issue with my express controller for deleting user posts. The problem is that while the post is being removed from the page successfully, it is not getting deleted from the User.posts data as expected.
function deleteRoute(req, res) {
Post
.findById(req.params.id)
.exec()
.then((post) => {
if(!post) return res.status(404).send('Not found');
return post.remove()
.then((thisUser)=>{
if (!Array.isArray(thisUser.posts)) {
thisUser.posts = [];
}
thisUser.posts.slice(post.id);
thisUser.save();
res.redirect(`/users/${req.user.id}`);
});
})
.catch((err) => {
res.status(500).end(err);
});
}
One major issue I'm facing is that when a post is created, the post count for the user increases accordingly. However, upon deletion of a post, the count doesn't seem to decrement as expected. I suspect the problem lies in trying to slice the post.id, but I'm unsure about the exact fix needed. Any assistance on resolving this would be greatly appreciated!