Type error not detected: the 'length' property of null can not be read (Uncaught TypeError: Can not read property 'length' of null)

0

function addRowDT(data) {
    var tabla = $("#tbl_pacientes").DataTable();
    for (var i = 0; i < data.length; i++) {
        tabla.fnAddData([
            data[i].IdPaciente,
            data[i].Nombres,
            (data[i].ApPaterno + " " + data[i].ApMaterno),
            ((data[i].Sexo == 'M')? "Masculino": "Femenino"),
            data[i].Edad,
            data[i].Direccion,
            ((data[i].Estado == true)? "Activo": "Inactivo")
        ]);
    }
}
    
asked by yunior prudencio moreno 27.12.2018 в 01:53
source

1 answer

1

The error is telling you that the variable you are using in the loop has a value of null, so you can not read its length property.

You should control this possible situation and only do the for in case it does not come to null

if (data!= null) {
--- código
}

I hope it serves you

    
answered by 27.12.2018 в 12:19