I’m looping through a large object where the internal properties are not all consistent. However, when I throw an if(typeof){} statement around the object, it’s still triggering an error:
The statement
if (typeof pendingTripDropoffMarkers[i].trip.id !== "undefined") {// do stuff}
The error
Uncaught TypeError: Cannot read property 'trip' of undefined
The catch
However, a sister object sitting in the same function with the same scope does just fine.
if (typeof pendingTripPickupMarkers[i].trip.id !== "undefined") {// do stuff}
If I do a print of object contents after they are created, they look just fine. So what evil force has a hand here? Thanks.
You should test any objects that have potential of being undefined.
If you’re really worried about
undefinedbeing overwritten, don’t do thetypeofhack. Just get a freshundefinedusing thevoidoperator.If you’re certain that
pendingTripDropoffMarkers[i]should be defined, then there’s some other issue somewhere.