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

1

I have a problem that is killing me ... I'm trying to get the API to be able to get the API data in the table, and it will give the error: Uncaught TypeError: Can not read property 'NAME' of undefined. And this "NAME" is an array that has in the API.

Follow the code:

botaoAdicionar.addEventListener("click", function(){
    var endereco = 'api aqui';
    $.ajax({
        url: endereco,
        complete: function(res){
            var meuJSON = JSON.parse(res.responseText);
            console.log(meuJSON);
            console.log("ola1")

            for (var i =0; i < 1; i++){
                console.log("ola2")
                adicionaPacienteNaTabela(meuJSON[i]);
                console.log("ola3")
           }
        }
    });
});

function montaTd(dado, classe) {
    var td = document.createElement("td");
    td.classList.add(classe);
    td.textContent = dado;

    return td;
}

function adicionaPacienteNaTabela(cliente) {

    var clienteTr = montaTr(cliente);
    var tabela = document.querySelector("#tabela-clientes");
    tabela.appendChild(clienteTr);
}

function montaTr(cliente) {
    var clienteTr = document.createElement("tr");
    clienteTr.classList.add("cliente");

    clienteTr.appendChild(montaTd(cliente.NOME, ".info-NOME"));

    return clienteTr;
}



 OS DADOS DO MEU API QUANDO COLOCO NO CONSOLE.LOG(MEUJSON); É:
{TRACKER: Array(2)}
TRACKER
:
Array(2)
0
:
CEPENT
:
"57000000"
CLIRET
:
"NAO"
CODCAR
:
"000152"
CODCLI
:
"526460"
CODPRO
:
"12819009       "
CPF
:
"02798477425   "
DESPRO
:
"COLCHAO GAZIN SUPREME COMPOSTO 138X188X21CM 1PL 660120 ./PR "
DTAENT
:
"20170715"
DTAENTORC
:
"20170707"
DTAMON
:
"20170724"
DTAMONORC
:
"20170712"
DTAPREENT
:
"20170714"
ENDENT
:
"R LUIZ DE MASCARENHA APT 1801           "
FILCD
:
"99"
FILORC
:
"01"
FLAG
:
"ENTREGUE          "
MUNENT
:
"MACEIO         "
NFISCA
:
"000033718"
NOME
:
"GUSTAVO SILVA E LINS                    "
NUMORC
:
"016350"
PEDCD
:
"110035"
QTDLIB
:
"1"
    
asked by anonymous 26.10.2017 / 15:30

1 answer

2

Your API response is returning something in the format:

//Cada PACIENTE é um JSON
{ TRACKER: [PACIENTE1, PACIENTE2] }

Then you have to adjust the parameter that is passed to function adicionaPacienteNaTabela

botaoAdicionar.addEventListener("click", function(){
var endereco = 'api aqui';
$.ajax({
    url: endereco,
    complete: function(res){
        var meuJSON = JSON.parse(res.responseText);
        console.log(meuJSON);
        console.log("ola1")

        for (var i =0; i < 1; i++){
            console.log("ola2")
            //Ajuste
            var paciente = meuJSON.TRACKER[i];
            adicionaPacienteNaTabela(paciente);

            console.log("ola3")
       }
    }
});
    
26.10.2017 / 17:02