1

I’m trying to create an error handler function for mongoose CastError. The error object doesn’t include a name field that indicates what type of error therefore I can’t run an if statement to check what type of error it is.mongoose error object

1
  • The id field is supposed to accept a MongoDB id. But I’m trying to code it in such a way that if the user enters a random string instead of the id, my CastError error handler will be triggered. But the problem here is that when the user enters a random string and mongoose throws an error I don’t have access to a name property to know what type of error it is Commented Jun 19, 2020 at 2:59

6 Answers 6

6

I have ran into the same error once.

Accessing err.name in a mongoose error handling middleware returned undefined.

I found out that err.name will only work if you are using the original err object returned from mongoose.

If you are making a copy of the object by Destructuring it like this:

let error = {...err};

this won't Include the name property. As a fix you can do this:

let error = {...err, name: err.name};

and it should work.

Idk why this happen but if anyone knows, please do let me know! Hope this helped you.

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

1 Comment

When you create a copy of an object using the { ...sourceObj } syntax, you have to keep in mind two things: (1) you are creating a shallow copy and (2) you will copy only enumerable attributes. Error.prototype.name is not enumerable therefore it will not be copied into the new object.
0

I came across the same issue while following Jonas' tutorial on node/express/mongoose. Perhaps at some point mongoose team decided to exclude 'name' property from 'error' object (this is just my guess, not a fact). Anyways, I overcame this issue as following: instead of checking the value of 'name' property I checked whether 'stack' property of the 'error' object starts with the words 'CastError'. If you use debugger and check all properties of 'error' object, you will notice that 'stack' property starts from the words 'CastError' when mongoose fails to use ID in the URL for its purposes (obviously when you use ID in the controller in that specific route)

in short instead of

if(err.name === 'CastError').....

I used

if(err.stack.startsWith('CastError')

Comments

0

I had the same problem,In my case I change the mongoose version

npm uninstall mongoose

npm i [email protected]

Comments

0

I am not sure if Jonas' tutorial has changed his way to do a hard copy. My way to solve this problem is let error = JSON.parse(JSON.stringify(err)); instead of let error = {...err}. All fields in the error could be perfectly copied and therefore used by other functions.

Comments

0
let error = Object.create(Object.getPrototypeOf(err));
Object.assign(error, err);

The name property is on prototype of err called MongooseError. Copy the prototype of err, or simply copy the err.name into error.

Comments

-1

I am not exactly sure what you are doing in your code but it looks like you are trying to basically pass a string value for the id field. But the string you are passing along is not a valid objectID and you can't pass a string value to the DB that is not a valid Object ID. You need to check if it's a valid object id first. You can do that by using

var ObjectID = require('mongodb').ObjectID;
ObjectID.isValid(YOUR_STRING_HERE);

You can check the official Documentation here I hope it helps.

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.