undefined n1 twitter
How To Fix 'Uncaught TypeError: Cannot read properties of undefined' - JavaScript Debuggingwhat's up
Updated on Jan 19,2023
How To Fix 'Uncaught TypeError: Cannot read properties of undefined' - JavaScript Debugging
what's up guys gary with self.dev today,we are going to go over how to fix,uncaught type error cannot read,properties of undefined,reading has cape or whatever property,you're trying to read,so let's go look at the code and see if,we can figure out what's going on here,in our scenario of this code we are,going to pretend like we made some api,requests and it's sending back,data um i watched the boys this weekend,so our,pretend data has to do with the boys but,we're going to say the api sends back,data about the boys right and it looks,like this it's got a name it's got their,powers and it's got a costume which,describes their costume and inside there,it's just going to have a boolean called,has cape and that's just whether they,have a cape or not,so,we're gonna pretend like we got this,back from an api,and then we want to,say data.for each so for each object,in,this array of data,if they have a cape basically we want to,console.log the hero name has a cape if,they don't then we want to console.log,hero name does not have a cape,now let's go through this line by line,so we can see kind of what's going on,and why this error is occurring,you might be able to just look at it and,be like oh i see why it's occurring but,we're gonna go through it just to,kind of explain it better so data.4h um,we're gonna call each individual object,hero so the first time through it grabs,this object and that is hero and then,we're going to say,hero.costume.hascape so we grab the,object,we look at costume,we look at hascape and that's true,so cool that's true we go inside the if,statement and we say hero.name so,homelander has a cape,and then if we look in here it console,logged homelander has a cape,next time we go through we grab this,second object,it says if hero.costum has cape,so look at the object hero,we look at costume we look it has cape,it's false so we do not go inside this,if statement we go to else and it,console logs hero.name which is the deep,hero name,does not have a cape,third time through we grab this object,and it says hero dot costume dot has,cape,so we look at hero,we say okay um we got hero,there's no costume on here so costume is,undefined,and then we try to get has cape on that,undefined property,and we get cannot read properties of,undefined because we're trying to read,properties of undefined like it's,when we say hero dot costume,it's basically hero dot costume,undefined like this,and,we're trying to access has cape on that,but it's undefined so it doesn't have,cape so thus we get there now how do we,fix that well,um one if you have control over the data,that's the easy or one of the easy,they're all easy ways actually um if you,have control over the data we can just,add costume and has cape and then save,and that will fix it and it will console,log all three things as we expect,but in our scenario um we're getting,this back from an api right so we can't,really control the data that comes back,we can just control what we do with the,data and how we handle that error,so we can use optional chaining that's,one thing um we can just add a question,mark here and that's going to say hey if,costume is it's basically saying if,costume is not undefined then let's try,and access hascape on it right,and then if we save here,we've removed the costume property from,this,and go back and it still works because,it's basically saying hero getting this,one,costume it's seeing that costume does,not exist on this it's undefined and,then it's saying all right cool that's,false so it breaks out of that and goes,to the else,another way you can do this if you i,think this,um was introduced in node like 13 so if,you have to use an older version of node,and you're not able to use this question,mark,a little more,tedious way to do it you can say hero,dot costume,and hero.costum it has cape,because,undefined which is,like when we try to read costume on hero,it's undefined and undefined is a falsy,value,so when it says hero.costume that's,false,and it says okay cool well that's false,this is and,so both of these would need to be true,for us to go in here this one's false,we're going to skip it and it goes down,to this and continues on and let's just,save and go look at our console we can,see there's no errors in there for that,and that's the third way to solve it but,hopefully you're using no or um like a,version that does support this,so,you can just use optional training that,makes it way easier and that'll solve,your problem,but hope this helps you out if you,learned something give me a thumbs up if,you have any questions um or comments,let me know in the description or the,comments below uh if you want to get my,resume template the one i used when i,was applying for tech jobs before i had,any tech experience a link to that is in,the description if you didn't come hang,out and discord and talk tech a link for,that is in the description as well and i,will see you guys next time peace
The above is a brief introduction to undefined n1 twitter
Let's move on to the first section of undefined n1 twitter
Let Tikstar's experts help you find the best TikTok product on your Shopify business!
Find Influencer (It's Free)
WHY YOU SHOULD CHOOSE Tikstar
Tikstar has the world's largest selection of social media to choose from, and each social media has a large number of influencer, so you can choose influencer for ads or brand marketing without any hassle.
undefined n1 twitter catalogs
- You Can Count by 1's to 150 | Jack Hartmann
- JavaScript Under The Hood [2] - Execution Context
- [CppIndia] Smart Pointers - Part 2 (std::shared_ptr & std::weak_ptr) by Shilpa Dodeja
- @rae: Existentials and writing functions for length-indexed vectors
- M1 MacBooks | Should You Buy 8GB or 16GB of RAM?
- Database In-Memory External Tables
- Elm - Michael Twomey