Uncaught Typeerror: Cannot read Property 'NOME' of Undefined

Asked

Viewed 1,655 times

1

I got a problem that’s killing me... I’m taking an API and putting it in a table there in the html, I made the Function to mount the td and everything, but when it arrives to put the API data in the table does not go and gives the error: Uncaught Typeerror: Cannot read Property 'NAME' of Undefined. And this "NAME" is an array you have in the API.

Follows 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"
  • Put the JSON coming from the API on that line: console.log(meuJSON);

  • I’m sorry, but I don’t understand

  • If you can post the JSON that comes from your API it is easier to help you. You already do this in your code within the function complete of Ajax on the line console.log(meuJSON).

  • I just edited the question, it has what comes now. Take a look please

1 answer

2


Your API response is returning something in format:

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

Then you have to adjust the parameter that is passed to the 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")
       }
    }
});
  • THANK YOU!!! That was really it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.