khushi2706 / Blog-App-using-MERN-stack

Blog App build with MERN stack
https://blog-app-using-mern-stack-beta.vercel.app
83 stars 125 forks source link

Delete blog is not working #1

Closed khushi2706 closed 12 months ago

swtbhsmn commented 1 year ago

Issue found :

{blogs &&
        blogs.map((blog, index) => (
          <Blog
            id={blog._id}
            isUser={localStorage.getItem("userId") === blog.user._id} // Always value of isUser is false
            title={blog.title}
            desc={blog.desc}
            img={blog.img}
            user={blog.user.name}
          />
))}

Explanation

Resolve

Replace this line {localStorage.getItem("userId") === blog.user._id} to {localStorage.getItem("userId") === blog.user}

Vyomrana02 commented 1 year ago

Hello @khushi2706 can u asign this issue to me I can fix this.

rohity20 commented 1 year ago

The existing code contains an error in the delete blog API, causing issues with the blog deletion functionality. I have implemented the following changes to rectify this issue.

const deleteBlog = async (req, res, next) => { const id = req.params.id;

try {
    const blog = await Blog.findByIdAndDelete(id).populate('user');

    if (!blog) {
        return res.status(404).json({ message: "Blog not found" });
    }

    // Remove the blog from the user's blogs array
    const user = blog.user;
    user.blogs.pull(blog);
    await user.save();

    return res.status(200).json({ message: "Successfully deleted" });
} catch (e) {
    console.error(e);
    return res.status(500).json({ message: "Unable to delete" });
}

}