Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
draw in the html data from a json array from api

draw in the html data from a json array from api:

{
"data": [
{
"_id": "126974b4-71f4-460d-a0b4-48f43d21e7c4",
"name": "jose",
"bet": 5,
"__v": 0
},
{
"_id": "c04515b7-8b25-4045-ba39-9cd19377a08b",
"name": "rigo",
"bet": 10,
"__v": 0
}
]
}

I get response from the api, in browser console

{data: Array(2)}

but I try to go through the obtained data and show it in the html, but it doesn't work, I put placeholder url and they work normally. I attach the code

code JS:

let url = `http://localhost:3000/api/v1/users`;
        fetch(url)
            .then( response => response.json() )
            .then( data => mostrarData(data) )
            .catch( error => console.log(error) )

        const mostrarData = (data) => {
            console.log(data)
            let body = ""
            for (var i = 0; i < data.length; i++) {      
               body+=`<tr><td>${data[i].id}</td><td>${data[i].name}</td><td>${data[i].email}</td></tr>`
            }
            document.getElementById('data').innerHTML = body
            //console.log(body)
        }
<div class="container mt-4 shadow-lg p-3 mb-5 bg-body rounded">
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Email</th>
        </tr>
      </thead>
      <tbody id="data">
      </tbody>
    </table>
  </div>

I try to traverse with map and for cycle, neither works, I don't know if the JSON has to perform a different search

note: I can't use JQuery, only JavaScript or axios

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You seem to miss that data object has data property. That's why the line data = data.data;

var data = {
  "data": [{
      "_id": "126974b4-71f4-460d-a0b4-48f43d21e7c4",
      "name": "jose",
      "bet": 5,
      "__v": 0
    },
    {
      "_id": "c04515b7-8b25-4045-ba39-9cd19377a08b",
      "name": "rigo",
      "bet": 10,
      "__v": 0
    }
  ]
}

const mostrarData = (data) => {
  // console.log(data)
  data = data.data;
  let body = ""
  for (var i = 0; i < data.length; i++) {
    body += `<tr><td>${data[i]._id}</td><td>${data[i].name}</td><td>${data[i].email}</td></tr>`
  }
  document.getElementById('data').innerHTML = body
  //console.log(body)
}

mostrarData(data)
<div class="container mt-4 shadow-lg p-3 mb-5 bg-body rounded">
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody id="data">
    </tbody>
  </table>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda