CastError: Cast to ObjectId failed for value "{ Id: '6035024ae0d98d49d0cec499' }" at path "_id" for model "posts" code example
Example 1: Cast to ObjectId failed for value
// it happens when you pass an invalid id to mongoose.
// so first check it before proceeding, using mongoose isValid function
import mongoose from "mongoose";
// add this inside your route
if( !mongoose.Types.ObjectId.isValid(id) ) return false;
Example 2: Cast to ObjectId failed for value
import mongoose from "mongoose";
// add this inside your route
if( !mongoose.Types.ObjectId.isValid(id) ) return false;