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

73
Visualizações
Fetch Api data is displaying undefined in html webpage but working in console
function getdata() {
  fetch(
    "https://api.lrs.org/random-date-generator?year=2015&source=api-docs"
  )
    .then((response) => response.json())
    .then((data) => data)
    .then(function (data) {
      let htmldata = "";
      htmldata = `<tr>
            <th>Quarter</th>
            <th>Day</th>
            <th>Month</th>
            <th>Date of Birth-</th>
            <th>Longest Day</th>
            <th>Unix</th>
            </tr>`;
           

let ndata = JSON.stringify(data);
      for (r in ndata) {
        
        htmldata += `<tr>
            <td>${r.quarter}-</td>
            <td>${r.day}-</td>
            <td>${r.month}-</td>
            <td>${r.db}-</td>
            <td>${r.long}-</td>
            <td>${r.unix}</td>
            </tr>`;
      
      }
      
      document.getElementById("rdata").innerHTML = htmldata;
    });
}

getdata();

data is displaying in the console but not stored in htmldata variable nor displaying in a webpage(showing undefined). Api data is showing in Objects in console not working in any html page. and data is looping but values doesn't get stored, maybe?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

The API returns an object with two properties, messages and data. You only need the data property.

After that, loop through the values of the data property with Object.values(), which turns the object into an iterable object that exposes the values of the properties that you can loop over.

function getdata() {
  fetch("https://api.lrs.org/random-date-generator?year=2015&source=api-docs")
  .then((response) => response.json())
  .then(({ data }) => data) // Return the data property
  .then(function (data) {
    let htmldata = `<tr>
      <th>Quarter</th>
      <th>Day</th>
      <th>Month</th>
      <th>Date of Birth-</th>
      <th>Longest Day</th>
      <th>Unix</th>
      </tr>
    `;

    for (const entry of Object.values(data)) {
      htmldata += `<tr>
        <td>${entry.quarter}-</td>
        <td>${entry.day}-</td>
        <td>${entry.month}-</td>
        <td>${entry.db}-</td>
        <td>${entry.long}-</td>
        <td>${entry.unix}</td>
        </tr>
      `;
    }
      
    document.getElementById("rdata").innerHTML = htmldata;
  });
}

getdata();
about 4 years ago · Santiago Gelvez 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