cannot read property

Solutions on MaxInterview for cannot read property by the best coders in the world

showing results for - "cannot read property"
Charlene
23 Oct 2019
1/* This error occurs in Chrome Browser when you read a property or call 
2a method on an undefined object. */
3// To handle it, use "Optional Chaining" compatible with Node.js v14.0.0
4
5Syntax -> ?.
6Usage ->
7let obj;
8console.log(obj.a) // Cannot read property 'a' of undefined
9console.log(obj?.a) // Works like a charm
Lavender
26 Jul 2018
1// That is mostly because the object was not declared until the moment when the code ran,
2// Example:
3
4
5console.log(object.list.map(el => el.name))
6
7const object = {
8	list: [
9    	{name: 'test'},
10        {name: 'second'}
11    ]
12}
13
14// The error happens because the object was created after the map call
15// The same happens if you forgot to import a package for example.
16
17// If you are dealing with a situation that you don't know if the object
18// has an specific property, you can use a question mark to tell the compiler
19// that maybe the property doesen't exists, returning undefined:
20
21console.log(object.default.size) // error
22console.log(object.list[1]?.age) // undefined
23console.log(object.default?.color) // undefined
24
Sofie
19 Apr 2016
1JavaScript 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 .
Erwan
29 May 2017
1Seems 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:
2
3var i1 = document.getElementById('i1');
4var i2 = document.getElementById('i2');
5var __i = {'user' : document.getElementsByName("username")[0], 'pass' : document.getElementsByName("password")[0] };
6if(i1 && i2 && __i.user && __i.pass)
7{
8    if(  __i.user.value.length >= 1 ) { i1.value = ''; } else { i1.value = 'Acc'; }
9
10    if(  __i.pass.value.length >= 1 ) { i2.value = ''; } else { i2.value = 'Pwd'; }
11}