Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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 !!!