I am trying to convert a String to ObjectId:
var _id=mongoose.Types.ObjectId(req.body.notebook);
The notebook value is actually the id of a document in the database:
> db.notebook.findOne()
{
"title" : "My Notebook",
"isActive" : false,
"_id" : ObjectId("54505ced1fa5b1b519bdfc88"),
"notes" : [ ],
"__v" : 0
}
I was debugging and the method looked like this:
mongoose.types.ObjectId("54505ced1fa5b1b519bdfc");
I got an error to this effect while debugging:
ObjectId must either be a 12 byte string or 24 hex charecters.
This error occurs from the method ObjectId.isValid.I have tried casting using:
mongoose.Types.ObjectId.fromString(req.body.notebook);
and
mongoose.mongo.BSONPure.ObjectID.fromString(req.body.notebook);
I understand that I have to convert this string to an appropriate hex format.How can I do that?