Can not convert undefined or null to object in Javascript

0

I am comparing an arrangement that comes to me from a query in the following way:

var data = response['return']; // Aquí se almacena el arreglo
var tableHead = '<tr>';
//Aquí comparo el arreglo para validar si esta vacío o indefinido o null

if(data === null || data === 'undefined' || data === ''){

  //Quiero imprimir este mensaje en el tbody de la tabla que estoy armando
  $tblReport.find('tbody').html('No hay datos disponibles');
}else{

//Aquí hago todo lo que necesito para formar mi tabla
 Object.keys(data[0]).each(function(prop){
  if(prop !== 'id_land' && prop !== 'id_product'){
    tableHead += '<th>'+prop+'</th>';
  }

 });
}

The code works very well, it arms me the table with the data, but when there is a data that does not exist according to the query, it does not validate the "if", but it directly goes to the else, and obviously it gives me an error because I am requesting the keys of the arrangement to form the table, and to avoid that I do that validation that if the array is empty I show the message in the body of the table.

I do not understand if I'm doing the comparisons wrong, since I've tried the following and it does not work for me:

if(typeof(data) === null || typeof(data) === 'undefined' || typeof(data) === ''){

      //Quiero imprimir este mensaje en el tbody de la tabla que estoy armando
      $tblReport.find('tbody').html('No hay datos disponibles');
    }else{

    //Aquí hago todo lo que necesito para formar mi tabla
     Object.keys(data[0]).each(function(prop){
      if(prop !== 'id_land' && prop !== 'id_product'){
        tableHead += '<th>'+prop+'</th>';
      }

     });
    }

or also:

if(data === null || data === 'undefined' || data === ''){

          //Quiero imprimir este mensaje en el tbody de la tabla que estoy armando
          $tblReport.find('tbody').html('No hay datos disponibles');

return null;
        }else{

        //Aquí hago todo lo que necesito para formar mi tabla
         Object.keys(data[0]).each(function(prop){
          if(prop !== 'id_land' && prop !== 'id_product'){
            tableHead += '<th>'+prop+'</th>';
          }

         });
        }
    
asked by Fabian Sierra 19.07.2016 в 18:23
source

3 answers

1

There are several things to keep in mind.

undefined is not enclosed in quotes, it is a reserved word.

> var data
> data === "undefined"
< false
> data === undefined
< true

However typeof , if the string returns "undefined" ...

> typeof data
< "undefined"
> typeof data === "undefined"
< true
> typeof data === undefined
< false

Not confusing? Well, if a bit, it is one of the slightly twisted features of javascript, also look at this table of truths how operators === and == behave. The operator == can sometimes give false positives depending on the data.

On the other hand, typeof null returns "object". It is something inherited from the first versions of javascript and there is not much to do. Therefore, it is not very useful.

The simplest thing would be to use

if (data) { 
   // no es null, ni undefined. 

} else {
   // es null o undefined, poner mensaje de "no hay datos"

}
    
answered by 19.07.2016 / 19:38
source
2

Summary of == and === to validate gaps

  • !a implies that a can be: null , undefined , false , 0 , "" (the empty string)
  • a == false is not the same as !a , because [] and "0" are == false but not using !a
  • a == null implies that a can be: null , undefined
  • a === null implies that a can be: null

Testing

To see how it works it is convenient to do some tests, for example:

[null, undefined, false, 0, '', [], {}, "0", true, 1].forEach(function(valor){
    console.log('el valor',JSON.stringify(valor));
    if(!valor)          console.log('!');
    if(valor == false)  console.log('==false');
    if(valor === false) console.log('===false');
    if(valor == null)   console.log('==null');
    if(valor === null)  console.log('===null');
    if(valor == 0)      console.log('==0');
    console.log('**********');
});
    
answered by 20.07.2016 в 14:36
0

I already solved it in the if where I was doing the comparison instead of "===" I put "==", like this:

if(data == null || data == 'undefined' || data == ''){

 $tblReport.find('tbody').html('No hay datos disponibles');

}

What I read is that Javascript doing this makes the values comparable and "===" is more strict with comparisons Greetings!

    
answered by 19.07.2016 в 18:35