I recently started using Express and am in the process of setting up the Delete function for my database within the MERN stack. While testing my CRUD operations using Insomnia, I have encountered an issue specifically with the Delete operation.
The problematic code block is as follows:
router.route('/:id').delete((res, req) => {
Post.findByIdAndDelete(req.params.id)
.then(() => res.json('Post deleted'))
.catch(err => res.status(400).json('Error: ' + err));
});
ERROR MESSAGE: TypeError: Cannot read property 'id' of undefined
What confuses me is that this same code successfully retrieves the req.params.id
.
router.route('/:id').get((req, res) => {
Post.findById(req.params.id)
.then(post => res.json(post))
.catch(err => res.status(400).json('Error: ' + err));
});
If anyone could provide assistance, I would greatly appreciate it.