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>';
}
});
}