What does the error "Uncaught TypeError: Can not set / read property 'XXX' of undefined / null mean and how to fix it?

10

The following code fails:

let obj1,obj2;
obj2 = {
  atributo: 'texto'
};

console.log(obj1.atributo);

Showing the message in console:

  

Uncaught TypeError: Can not read property 'attribute' of undefined

What does it mean and how can I solve it?

    
asked by Pablo Lozano 30.05.2018 в 17:39
source

2 answers

6

Based on the error

  

Uncaught TypeError: Can not read property 'attribute' of undefined

The error means that you can not access that property , but why?

In the example that you placed obj1 is declared but not used, let's see the type of data that the test gives us

let obj1,obj2;
obj2 = {
  atributo: 'texto'
};
console.log("obj1->" , typeof(obj1));
console.log("obj2->" , typeof(obj2));

How to fix it?

To give this error they must be null or undefined , what you can do is the following:

  • Verify if the object is declared
  • The assignment to your Object does not receive null or undefined parameters
  • If you use an external library verify that it is well imported

let libreriaExterna 
libreriaExterna.top = 3;
  • Validate if the attribute exists (in the worst case that your object is very variable)

let obj1 ,obj2
    obj2 = {
      atributo: 'texto'
    };
    console.log("obj1.atributo?" , obj1.hasOwnProperty('atributo'));
    console.log("obj2.atributo?" , obj2.hasOwnProperty('atributo'));
  • Control them with a Try / Cath (Javascript has that? yes)

let obj1,obj2;
obj2 = {
  atributo: 'texto'
};
try{
console.log(obj1.atributo);
}catch(e){
console.error(e.message,"no existe el atributo");

}
    
answered by 30.05.2018 / 19:06
source
6

Following the series of questions and answers of the same theme

We will clarify its equivalent in Javascript:

What does it mean?

The meaning of the error message "Uncaught TypeError: Can not read property 'XXX' of undefined" is:

  

Error TypeError not captured: I can not read property 'XXX' of undefined .

And it means that we are trying to access a property (attribute or function) of an identifier that is not defined.

In the example of the question the error occurs because we have two variables, obj1 and obj2, where a obj1 has not been assigned any value, it is undefined . If obj1 were worth null , the error would be similar.

How to fix it?

The way to solve it is to look in our code for the attribute XXX that you try to access (or define) and then see why the parent object is null or undefined .

Typical reasons are usually:

  • Try to access a variable modified by asynchronous code (the response to an AJAX call or an event such as a mouse click or the pressing of a key) before it has assigned a value to said variable.

  • Assume that a function always returns a value other than null or undefined : for example, if this query returns a list with 2 elements, but we are trying to access a nonexistent third element:

    let myDivs = document.getElementsByClass('myDiv');
    let text=myDivs[2].innerText;
    // Uncaught TypeError: Cannot read property 'innerText' of undefined
    
answered by 30.05.2018 в 17:39