complete-node-bootcamp
complete-node-bootcamp copied to clipboard
Can't find the name property inside the err object.
It seems like Express removed the name property inside the err object. Can anyone confirm this?
This is how it looks like on my Postman
{
"status": "error",
"error": {
"stringValue": "\"5f9618b492f2d81f5cb032829\"",
"kind": "ObjectId",
"value": "5f9618b492f2d81f5cb032829",
"path": "_id",
"reason": {},
"statusCode": 500,
"status": "error"
},
"message": "Cast to ObjectId failed for value \"5f9618b492f2d81f5cb032829\" at path \"_id\" for model \"Tour\"",
"stack": "CastError: Cast to ObjectId failed for value \"5f9618b492f2d81f5cb032829\" at path \"_id\" for model \"Tour\"\n at model.Query.exec (C:\\Users\\SayJee\\OneDrive\\Desktop\\Nodles\\complete-node-bootcamp-master\\4-natours\\practice\\node_modules\\mongoose\\lib\\query.js:4352:21)\n at model.Query.Query.then (C:\\Users\\SayJee\\OneDrive\\Desktop\\Nodles\\complete-node-bootcamp-master\\4-natours\\practice\\node_modules\\mongoose\\lib\\query.js:4444:15)\n at processTicksAndRejections (internal/process/task_queues.js:97:5)"
}
As you can see in the code above, there is no 'name' property,, therefore we can't use this code below anymore and it's outdated.
if(error.name === 'CastError') error = handleCastErrorDB(error);
i even tried to make a console.log(error.name) and you know what? it returns undefined.
Is there an alternative solution for this? Thanks for checking out.
Yup the same is happening with me... @saysonjerald. @jonasschmedtmann Can u please confirm this and update it in the video too..
I think i understand what is happening here..
You See the functions in our ErrorController.js handleCastErrorDB() OR handleDuplicateFieldsDB() are thrown by Mongoose or MongoDB (Official Doc Link for Error's coming from mongoDB) and propbably it is MongoDb who must have stopped emitting error.name value and switched to StatusCodes instead as seen in the Official Doc Link.
In our ErrorController.js we do have some errors checked by statuscode before we call the custom functions in the if loops such as this one if (error.code === 11000) error = handleDuplicateFieldsDB(error);
In Code shared by @saysonjerald you can see that we do have statuscode, Also in the "stack": you can see we have a string starting with "CastError" I think they must have done it to prevent information leakage while in prod mode as stack is mostly used during development and not during Prod.
Quick Fix can be to completly switch over to statusCodes
Option 1
Inside our errorController.js, call the custom functions in the If statements with err.statusCode instead of err.name
Option 2 (Long Way)
We can Take the String inside of the stack and check for the string starting value with the previous names we already have defined in our if block
Or a combination of both...
I fixed it using the 2nd option suggested by @nvispute
I put this code after the error variable declaration:
if (err.stack.indexOf('CastError', 0) === 0) error.name = 'CastError';
Hope it works for you
I think i understand what is happening here.. You See the functions in our ErrorController.js
handleCastErrorDB()ORhandleDuplicateFieldsDB() are thrown by Mongoose or MongoDB (Official Doc Link for Error's coming from mongoDB) and propbably it is MongoDb who must have stopped emitting error.name value and switched to StatusCodes instead as seen in the Official Doc Link. In our ErrorController.js we do have some errors checked by statuscode before we call the custom functions in the if loops such as this oneif (error.code === 11000) error = handleDuplicateFieldsDB(error);In Code shared by @saysonjerald you can see that we do have statuscode, Also in the "stack": you can see we have a string starting with "CastError" I think they must have done it to prevent information leakage while in prod mode as stack is mostly used during development and not during Prod.
Quick Fix can be to completly switch over to statusCodes Option 1 Inside our errorController.js, call the custom functions in the
Ifstatements with err.statusCode instead of err.name Option 2 (Long Way) We can Take the String inside of the stack and check for the string starting value with the previous names we already have defined in our if blockOr a combination of both...
I don't think they change the error object. The name property is still there. What actually happen is because we are simply shallow copying the err object using spread operator. The problem is that the name property is inside the prototype of the err object but copying with spread operator returns a new object that does not include the prototype of the object that it is copying. The solution is to use Object.create() instead of the spread operator

sources:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals
Thanks for the solution, I was frustrated for an hour trying to think what was wrong, turns out this is one of those occasions where destructuring backfires.
@amiraiman. Thanks for the help.
I think i understand what is happening here.. You See the functions in our ErrorController.js
handleCastErrorDB()ORhandleDuplicateFieldsDB() are thrown by Mongoose or MongoDB (Official Doc Link for Error's coming from mongoDB) and propbably it is MongoDb who must have stopped emitting error.name value and switched to StatusCodes instead as seen in the Official Doc Link. In our ErrorController.js we do have some errors checked by statuscode before we call the custom functions in the if loops such as this oneif (error.code === 11000) error = handleDuplicateFieldsDB(error);In Code shared by @saysonjerald you can see that we do have statuscode, Also in the "stack": you can see we have a string starting with "CastError" I think they must have done it to prevent information leakage while in prod mode as stack is mostly used during development and not during Prod. Quick Fix can be to completly switch over to statusCodes Option 1 Inside our errorController.js, call the custom functions in theIfstatements with err.statusCode instead of err.name Option 2 (Long Way) We can Take the String inside of the stack and check for the string starting value with the previous names we already have defined in our if block Or a combination of both...I don't think they change the error object. The name property is still there. What actually happen is because we are simply shallow copying the err object using spread operator. The problem is that the name property is inside the prototype of the err object but copying with spread operator returns a new object that does not include the prototype of the object that it is copying. The solution is to use Object.create() instead of the spread operator
sources:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals
Thanks @amiraiman. Your answer helped however I got an empty error object in case of a duplicated field in mongodb. And here what I tried:
let error = Object.create(err, Object.getOwnPropertyDescriptors(err));
This adds the rest of properties of err object, after those in the prototype.
Know more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
Hey there,
This is an automatic reply. I know no one likes to get these, but I get an incredible amount of email, and I'd rather spend my time doing what I do best: working on bringing you the best courses I possibly can 😎
So the following points should reply to 90% of emails I get:
[1] FOR STUDENTS
👉 Do you have a problem with one of my courses? Please post it in the Q&A section, that's where all problems will get solved 😃 👉 Are you looking for career advice/next steps? Again, the course Q&A is the place to go. There, everyone can benefit from replies. 👉 Can't find your certificate? Please see this articlehttps://support.udemy.com/hc/en-us/articles/229603868-How-to-Download-Your-Certificate-of-Completion-on-a-Browser-. 👉 Are you looking for an e-book? The e-book was replaced many years ago by my resources pagehttps://codingheroes.io/resources/. 👉 Are you looking to get a course for free? I sometimes run giveaways on my Discord channelhttps://discord.gg/uhMkpf4, but in general I don't give out free coupons, simply because it's unfair to people who actually bought the course. 👉 Are you looking for my older newsletters? I don't have them on my website currently. Maybe I will at some point, so check my website again later.
[2] FOR MARKETING PEOPLE
👉 Do you want me to publish my courses on your platform/create new courses/write a book/etc.? I won't, I am working 100% exclusive with Udemy. 👉 Do you want to help me with marketing my courses? I'm not interested, Udemy does a stellar job at promoting (really, don't waste your time trying to convince me).
If you have something important that's not covered here, just reply to this email, and I might answer it at some point (I can't guarantee it 😅).
Have a wonderful day!
Jonas
On January 2, 2023 at 7:07 PM UTC, Abissa @.***) wrote:
I think i understand what is happening here.. You See the functions in our ErrorController.js handleCastErrorDB() OR handleDuplicateFieldsDB() are thrown by Mongoose or MongoDB (Official Doc Link for Error's coming from mongoDBhttps://docs.atlas.mongodb.com/api#errors) and propbably it is MongoDb who must have stopped emitting error.name value and switched to StatusCodes instead as seen in the Official Doc Link. In our ErrorController.js we do have some errors checked by statuscode before we call the custom functions in the if loops such as this one if (error.code === 11000) error = handleDuplicateFieldsDB(error); In Code shared by @saysonjeraldhttps://github.com/saysonjerald you can see that we do have statuscode, Also in the "stack": you can see we have a string starting with "CastError" I think they must have done it to prevent information leakage while in prod mode as stack is mostly used during development and not during Prod. Quick Fix can be to completly switch over to statusCodes Option 1 Inside our errorController.js, call the custom functions in the If statements with err.statusCode instead of err.name Option 2 (Long Way) We can Take the String inside of the stack and check for the string starting value with the previous names we already have defined in our if block Or a combination of both...
I don't think they change the error object. The name property is still there. What actually happen is because we are simply shallow copying the err object using spread operator. The problem is that the name property is inside the prototype of the err object but copying with spread operator returns a new object that does not include the prototype of the object that it is copying. The solution is to use Object.create() instead of the spread operator
sources:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/name
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#spread_in_object_literals
Thanks @amiraimanhttps://github.com/amiraiman. Your answer helped however I got an empty error object in case of a duplicated field in mongodb. And here what I tried:
let error = Object.create(err, Object.getOwnPropertyDescriptors(err));
This adds the rest of properties of err object, after those in the prototype.
— Reply to this email directly, view it on GitHubhttps://github.com/jonasschmedtmann/complete-node-bootcamp/issues/55#issuecomment-1369152829, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEO2BAQGAXTID4KE3N6SKPDWQMRO5ANCNFSM4TXCJPTA. You are receiving this because you were mentioned.Message ID: @.***>
Abissa, thank you, this finally solved it for me aftwe days of trying to debug this!
Just to clarify, using the suggested solution of : let error = Object.create(err)
This solved it!