I want to create a mongoID using an URI fragment as source. However, unless the URI really contains a properly formatted mongoId, I get the below error thrown.
What should I change to allow _id creation without throwing errors as wrapping it in a try/catch block doesn't do it?
Error:
500 Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format
The code:
var _id = db.bson_serializer.ObjectID.createFromHexString(req.params.id);
this.db.users.findById(_id, function(err, doc) {
if (!err && !!doc) { res.send(JSON.stringify(doc)); } else {res.send(JSON.stringify({error: "Not found"));}
});
I use the mongoskin driver.
/users/nonvalidid- throws error./users/47cc67093475061e3d95369d- valid.