I am purposely passing an incomplete id to New ObjectId() while using Mongodb with node.js.
As a result, I get the following error:
BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
I am catching the error in a try/catch. I can console.log the error. However, when I use res.send(error) to receive the error message in the client side(postman), I receive an empty object.
I would like to know what would be the proper technique to catch that particular error and send it to the client.
This is a simplified version of my code:
try{
const userId = new ObjectId('6w8268bkdkw') //wrong Id
}
catch{
res.send(error)
}
2
Answers
You are not using try/catch correctly:
In your route handler function you can code like this, for example: