Uncaught Typeerror Cannot Read Property of Undefined
1 of the most common type errors in JavaScript is the famous "Cannot read belongings of undefined". This error occurs when you try to read or access a property on an object that is undefined. Another mutual case that is caused by a like effect, is when you lot get the same mistake bulletin, but with goose egg instead of undefined.
Cannot read holding of zero
Why does this happen?
Imagine the following situation. Yous have a user object that is initially undefined, and it is populated through a fetch request. Your server is down and and so information technology returns with an undefined value, but you didn't handle this failure path, and you still try to read backdrop from the user object:
let user; // The variable is set to undefined user = await getUser (id) ; // The request fails and returns `undefined`, but it is not handled console. log (user.name) ; // Y'all try to log the `proper name` holding of the `user` object, that is still `undefined` Copied to clipboard!
The variable in the code instance above is declared, but its value is yet prepare to undefined. Hither you are essentially trying to practise the following:
console. log ( undefined .name) ; // This will throw "Cannot read holding 'name' of undefined" // Same as if the asking returns with a `zero` and you try to read properties from that panel. log ( null .name) ; // This will throw "Cannot read belongings 'name' of null" Copied to clipboard!
Since undefined is not an object, you will become a TypeError, like the one below. So how can we avert this?
Fugitive errors
To avoid getting these types of errors, we need to brand sure that the variables we are trying to read practice have the correct value. This tin be washed in various ways. We can do if checks earlier dealing with objects whose values are bound to modify:
if (user !== undefined ) { // Here `user` is surely not `undefined` } if ( typeof (user) !== 'undefined' ) { // We tin also utilise the `typeof` operator } Copied to clipboard!
A much cleaner solution even so is to use the logical OR operator, when you assign a value to a variable, or fifty-fifty better, when you return the value from the function:
// Assign a fallback during declaration user = getUser (id) || { } ; // Assign a fallback during return const getUser = id => { ... return userResponse || { } ; } ; Copied to clipboard!
If getUser returns undefined or null, then we can fall back to an empty object, and assign that to the user variable. This way, if we attempt to access user.name, nosotros will get undefined, as nosotros don't have that holding on the user object, merely nosotros all the same have an object to work with, then nosotros don't get an error. We can likewise use TypeScript to easily spot these types of mistakes right within our IDE.
Resources:
- Logical OR operator
- The
typeofoperator
📚 Get admission to exclusive content
Want to get access to exclusive content? Support webtips to become access to tips, checklists, cheatsheets, and much more. ☕
Go admission
Courses
championrusestiond.blogspot.com
Source: https://www.webtips.dev/webtips/javascript/avoid-getting-cannot-read-property-of-undefined-in-javascript
0 Response to "Uncaught Typeerror Cannot Read Property of Undefined"
Postar um comentário