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

102
Visualizações
HTML content is not added

I have a similar class structure and it doesn't work for me, I've already tried several things and can't fix the problem. As you can see, the constructors are executed correctly and also the method executed in the last constructor. However, when I create HTML content, it doesn't paint it. Why and how could you solve this?

class AutoComplete{
    constructor(){
    console.log("constructor autocomplete")
    this.table = new Table();
    
  }
}

class Table{
    constructor(){
    console.log("constructor table")
    
    this.arr = []
    
    fetch('https://jsonplaceholder.typicode.com/posts')
    .then((response) => response.json())
    .then((data) => {
        data.map(d => this.arr.push(d))
    });
  
    this.fill();
  }
  
  fill = () => {
    console.log("fill");
    const content = document.querySelector("#content");
    // doesn't work
    this.arr.forEach( ct => {
        const div = document.createElement("div");
        div.innerText = ct.body;
      
        content.appendChild(div);
        //content.innerHTML += div;
    });
  }
}

let autoc = new AutoComplete();
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title></title>
</head>
<body>
  
  <div id="content"></div>

</body>
</html>

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

0

This is happening because you need to call this.fill() within the .then() callback function. Otherwise. this.fill is called before you get data back from the API.

Demo:

class AutoComplete{
    constructor(){
    console.log("constructor autocomplete")
    this.table = new Table();
    
  }
}

class Table{
    constructor(){
    console.log("constructor table")
    
    this.arr = []
    
    fetch('https://jsonplaceholder.typicode.com/posts')
    .then((response) => response.json())
    .then((data) => {
        data.map(d => this.arr.push(d));
        this.fill();
    })
    
    // this.fill()
  }
  
  fill = () => {
    console.log("fill");
    const content = document.querySelector("#content");
    // doesn't work
    this.arr.forEach(ct => {
        const div = document.createElement("div");
        div.innerText = ct.body;
      
        content.appendChild(div);
        //content.innerHTML += div;
    });
  }
}

let autoc = new AutoComplete();
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title></title>
</head>
<body>
  
  <div id="content"></div>

</body>
</html>

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