cannot read property

/* This error occurs in Chrome Browser when you read a property or call 
a method on an undefined object. */
// To handle it, use "Optional Chaining" compatible with Node.js v14.0.0

Syntax -> ?.
Usage ->
let obj;
console.log(obj.a) // Cannot read property 'a' of undefined
console.log(obj?.a) // Works like a charm

4.1
10
Phoenix Logan 186120 points

                                    Seems like one of your values, with a property key of 'value' is undefined. Test that i1, i2and __i are defined before executing the if statements:

var i1 = document.getElementById('i1');
var i2 = document.getElementById('i2');
var __i = {'user' : document.getElementsByName("username")[0], 'pass' : document.getElementsByName("password")[0] };
if(i1 && i2 && __i.user && __i.pass)
{
    if(  __i.user.value.length >= 1 ) { i1.value = ''; } else { i1.value = 'Acc'; }

    if(  __i.pass.value.length >= 1 ) { i2.value = ''; } else { i2.value = 'Pwd'; }
}

4.1 (10 Votes)
0
4.75
4
Awgiedawgie 440220 points

                                    JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. This error occurs in Chrome Browser when you read a property or call a method on an undefined object .JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. This error occurs in Chrome Browser when you read a property or call a method on an undefined object .

4.75 (4 Votes)
0
0
5
Awgiedawgie 440220 points

                                    // That is mostly because the object was not declared until the moment when the code ran,
// Example:


console.log(object.list.map(el => el.name))

const object = {
	list: [
    	{name: 'test'},
        {name: 'second'}
    ]
}

// The error happens because the object was created after the map call
// The same happens if you forgot to import a package for example.

// If you are dealing with a situation that you don't know if the object
// has an specific property, you can use a question mark to tell the compiler
// that maybe the property doesen't exists, returning undefined:

console.log(object.default.size) // error
console.log(object.list[1]?.age) // undefined
console.log(object.default?.color) // undefined

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source