Currently, I am following coursework where I have successfully implemented a delete function that removes data from the node server. The setup involves an Angular frontend connected to a MongoDB database.
The code for the 'deletePost' function in the Angular service:
deletePost(postId: string) {
this.http
.delete('http://localhost:3000/api/posts/' + postId)
.subscribe(() => {
console.log('deleted');
const updatedPosts = this.posts.filter(post => post.id !==
postId);
this.posts = updatedPosts;
this.postsUpdated.next([...this.posts]);
});
}
For the Angular front end:
onDelete(postId: string) {
console.log('deleted');
this.postsService.deletePost(postId);
After testing the functionality, it seems that upon clicking the delete button for the first time, the action is initiated visually but no data deletion occurs until a page refresh and subsequent attempt. Despite seeing the 'deleted' log from the frontend, the actual post removal process only triggers on the second attempt.
Regarding the deletion process on the node server side:
app.delete('/api/posts/:id', (req, res, next) => {
Post.deleteOne({ _id: req.params.id }).then(result => {
res.status(200).json({ message: 'Post deleted' });
});
});
How can this issue be resolved effectively?
Could the delay in immediate deletion perhaps hint at a waiting period or specific order of operations needed for proper execution? Consider adding additional instructions or validations to streamline the deletion process.