project_mern_memories
project_mern_memories copied to clipboard
Part 2: TypeError: PostMessage.findByIdAndRemove is not a function
- Encountered the following error when I clicked the delete button.
TypeError: PostMessage.findByIdAndRemove is not a function
- 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." });
}
-
Cause: As of Mongoose version 8 there is no more .findByIdAndRemove() in its place you will have to use .findByIdAndDelete() Model.findByIdAndDelete()
-
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." });
}
I've create a pull request #184 addressing this change 😄