Skip to content Skip to sidebar Skip to footer

Typeerror: Cannot Read Property 'available' of Undefined

How to Avoid Getting "Cannot read property of undefined" in JavaScript

1 of the virtually common type errors in JavaScript is the famous "Cannot read holding of undefined". This fault occurs when you lot try to read or access a holding on an object that is undefined. Another mutual case that is caused by a like issue, is when you lot become the same error message, simply with null instead of undefined.

Cannot read holding of nothing
Learn JavaScript with Educative

Why does this happen?

Imagine the following state of affairs. You have a user object that is initially undefined, and it is populated through a fetch request. Your server is down and so it returns with an undefined value, but you didn't handle this failure path, and you lot still endeavour to read properties from the user object:

                          let              user;              // The variable is set to undefined              user              =              wait              getUser              (id)              ;              // The request fails and returns `undefined`, simply it is not handled              console.              log              (user.proper noun)              ;              // You attempt to log the `proper name` property of the `user` object, that is still `undefined`                      

Copied to clipboard!

The variable in the code example above is declared, but its value is withal fix to undefined. Here you are substantially trying to do the following:

            console.              log              (              undefined              .name)              ;              // This will throw "Cannot read property 'proper name' of undefined"              // Same as if the request returns with a `nothing` and you endeavor to read properties from that              console.              log              (              null              .name)              ;              // This will throw "Cannot read property 'name' of null"                      

Copied to clipboard!

Since undefined is non an object, you will get a TypeError, similar the one below. So how can nosotros avert this?

Getting the cannot read property of undefined error inside the console

Avoiding errors

To avoid getting these types of errors, we need to make sure that the variables we are trying to read do have the correct value. This tin can exist done in diverse means. We can practice if checks before dealing with objects whose values are bound to modify:

                          if              (user              !==              undefined              )              {              // Here `user` is surely not `undefined`              }              if              (              typeof              (user)              !==              'undefined'              )              {              // We tin can also employ the `typeof` operator              }                      

Copied to clipboard!

A much cleaner solution however is to utilise the logical OR operator, when yous assign a value to a variable, or even better, when you return the value from the role:

                          // Assign a fallback during annunciation              user              =              getUser              (id)              ||              {              }              ;              // Assign a fallback during return              const              getUser              =              id              =>              {              ...              return              userResponse              ||              {              }              ;              }              ;                      

Copied to clipboard!

If getUser returns undefined or zip, and so we can fall dorsum to an empty object, and assign that to the user variable. This way, if we try to admission user.name, we will get undefined, every bit nosotros don't have that property on the user object, only we nonetheless have an object to work with, so nosotros don't get an error. We tin can likewise apply TypeScript to easily spot these types of mistakes correct within our IDE.

If you would like to run into more webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • Logical OR operator
  • The typeof operator

📚 Get access to exclusive content

Want to get access to sectional content? Support webtips to go access to tips, checklists, cheatsheets, and much more. ☕

Become admission Support us

Courses

Read more on

robertsonstord1974.blogspot.com

Source: https://www.webtips.dev/webtips/javascript/avoid-getting-cannot-read-property-of-undefined-in-javascript

Post a Comment for "Typeerror: Cannot Read Property 'available' of Undefined"