2

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?

1 Answer 1

3

"54505ced1fa5b1b519bdfc" - is only 22 characters long. It's incomplete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.