Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am Unable to delete my post from mongo DB , or most specific await post.delete(); not working #2643

Open
anandkunalmishra opened this issue Apr 7, 2023 · 2 comments

Comments

@anandkunalmishra
Copy link

Notes

Please delete this section after reading it

If you have a problem with an error message:

  • node version: <run "node -v" in you terminal and replace this>
  • npm version: <run "npm -v" in you terminal and replace this>
  • os: windows 7/windows 10/mac/linux

Error output

 // copy your error output here

await post.delete(); not working

My Code

 // copy your code here
router.delete("/:id", async (req, res) => {
    try {
      const post = await Post.findById(req.params.id);
      if(post.username === req.body.username){
        try {
            console.log("executing try block ");
            await post.delete();
            res.status(200).json("Post has been deleted ...");
         } 
         catch (error) {
            res.status(500).json(error);
            }
        }
      else {
          res.status(401).json({message:"You can delete only your post "});
      }
    } catch (error) {
  
        res.status(500).json(error);
    }
  });
@anandkunalmishra
Copy link
Author

when I am running on postman , the json I am using is correct but it showing status 500 means I think await post.delete() not working

@mahdi-eth
Copy link

Potential Solutions and Suggestions

  1. Post Existence Verification:
    Ensure that the Post.findById(req.params.id) call returns a valid post object. If it returns null or undefined, it might indicate an issue with the ID or the post's non-existence.

  2. Error Handling:
    Verify that the error handling within the nested try...catch block accurately captures any potential errors thrown during the deletion process.

  3. Mongoose Model Method:
    Confirm that the post object retrieved by Post.findById() supports the .delete() method. For MongoDB/Mongoose, it might be necessary to use methods like .remove() or deleteOne() instead.

Here's an updated version of the code that considers these points:

router.delete("/:id", async (req, res) => {
    try {
        const post = await Post.findById(req.params.id);
        if (!post) {
            return res.status(404).json({ message: "Post not found" });
        }

        if (post.username === req.body.username) {
            try {
                console.log("Executing try block");
                await post.remove(); // Or use post.deleteOne() if appropriate
                res.status(200).json({ message: "Post has been deleted" });
            } catch (error) {
                res.status(500).json({ message: "Error deleting post", error });
            }
        } else {
            res.status(401).json({ message: "You can delete only your post" });
        }
    } catch (error) {
        res.status(500).json({ message: "Server error", error });
    }
});

Please ensure that the provided req.params.id and req.body.username match the expected data types and values. Also, validate the Mongoose model's method used for deletion according to your schema setup.

Check your server logs or console for any error messages or logs generated during the request for further debugging.

Let me know if you need further assistance or clarification on this matter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants