JavaScript Building a MEAN Application Creating and Editing Data in a MEAN App Editing Data with PUT Routes in Express

Tolgay Budayici
Tolgay Budayici
4,362 Points

"message": "Cast to ObjectId failed for value \"<ObjectId>\" at path \"_id\" for model \"Todo\""

Hey guys,

On postman I get this error message when trying to update my todo: "message": "Cast to ObjectId failed for value \"<ObjectId>\" at path \"_id\" for model \"Todo\""

Anyone knows why I get this message ? ... and how to solve this problem ?

Here is my code !

router.put('/todos/:id', function (req, res) {
    var id = req.params.id;
    var todo = req.body;
    if (todo && todo._id !== id) {
        return res.status(500).json({err: "Ids do not match"});
    }
    Todo.findByIdAndUpdate(id, todo, {new: true}, function (err, todo) {
        if (err) {
            return res.status(500).json({message: err.message});
        }
        res.json({'todo': todo, message: "Todo updated"});
    });

Cheers !!!