TheXanderSavage
TheXanderSavage

Reputation: 43

Can’t find name property in mongoose error function

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

Upvotes: 1

Views: 1312

Answers (6)

Rowan7
Rowan7

Reputation: 1

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.

Upvotes: 0

B_flee
B_flee

Reputation: 13

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.

Upvotes: 0

shm19
shm19

Reputation: 1

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

npm uninstall mongoose

npm i [email protected]

Upvotes: 0

Divyansh Acharya
Divyansh Acharya

Reputation: 76

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.

Upvotes: 6

esentai
esentai

Reputation: 69

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')

Upvotes: 0

Rajat Mangal
Rajat Mangal

Reputation: 9

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.

Upvotes: -1

Related Questions