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

Part 2: TypeError: PostMessage.findByIdAndRemove is not a function #182

Open
AAdewunmi opened this issue Jan 7, 2024 · 1 comment
Open

Comments

@AAdewunmi
Copy link

  1. Encountered the following error when I clicked the delete button.

TypeError: PostMessage.findByIdAndRemove is not a function

  1. Source:
server/controllers/post.js - deletePost()
export const deletePost = async (req, res) => {
    const { id } = req.params;
    if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
    await PostMessage.findByIdAndRemove(id); **<== Error Source ===>** 
    res.json({ message: "Post deleted successfully." });
}
  1. Cause:
    As of Mongoose version 8 there is no more .findByIdAndRemove() in its place you will have to use .findByIdAndDelete()
    Model.findByIdAndDelete()

  2. Fix: use findByIdAndDelete()

export const deletePost = async (req, res) => {
  const { id } = req.params;
  if (!mongoose.Types.ObjectId.isValid(id))
    return res.status(404).send(`No post with id: ${id}`);
  await PostMessage.findByIdAndDelete(id); **<== Fix ===>** 
  res.json({ message: "Post deleted successfully." });
}
Goldfish7718 added a commit to Goldfish7718/project_mern_memories that referenced this issue Jan 13, 2024
@Goldfish7718
Copy link

I've create a pull request #184 addressing this change 😄

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

No branches or pull requests

2 participants