Solucionado (ver solução)
Solucionado
(ver solução)
5
respostas

Uncaught TypeError: Cannot set property 'innerHTML' of null

O meu código não carrega a tabela do HTML dentro da template

class NegociacoesView{

      constructor(elemento){
          this._elemento = elemento;
      } 

    _template(){
        return `
        <table class="table table-hover table-bordered">
        <thead>
            <tr>
                <th>DATA</th>
                <th>QUANTIDADE</th>
                <th>VALOR</th>
                <th>VOLUME</th>
            </tr>
        </thead>

        <tbody>
        </tbody>

        <tfoot>
        </tfoot>
    </table>
        `;
    }
    update() {

        this._elemento.innerHTML = this._template();
  }
}
5 respostas

o código do controller

class NegociacaoController {

  constructor() {
    let $ = document.querySelector.bind(document);
    this._inputData = $('#data');
    this._inputQuantidade = $('#quantidade');
    this._inputValor = $('#valor');
    this._listaNegociacoes = new listaNegociacoes();
    this._negociacoesView = new NegociacoesView($('#negocicoesView'));
    this._negociacoesView.update();
  }


  adiciona(event){
    event.preventDefault();

    this._listaNegociacoes.adiciona(this._criaNegociacao());
    this._limpaFormulario();
    console.log(this._listaNegociacoes.negociacoes);
  }
    _criaNegociacao(){
       return new Negociacao(
       DateHelper.textoParaData(this._inputData.value),
       this._inputQuantidade.value,
       this._inputValor.value);
    }
    _limpaFormulario(){
      this._inputData.value = '';
      this._inputQuantidade.value = 1;
      this._inputValor.value = 0.0;

      this._inputData.focus();
    }
}

o código do HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Negociações</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/bootstrap-theme.css">

</head>
<body class="container">

    <h1 class="text-center">Negociações</h1>

    <form class="form" onsubmit="negociacaoController.adiciona(event)">

        <div class="form-group">
            <label for="data">Data</label>
            <input type="date" id="data" class="form-control" required autofocus/>
        </div>

        <div class="form-group">
            <label for="quantidade">Quantidade</label>
            <input type="number" min="1" step="1" id="quantidade" class="form-control" value="1" required/>
        </div>

        <div class="form-group">
            <label for="valor">Valor</label>
            <input id="valor" type="number" class="form-control"  min="0.01" step="0.01" value="0.0" required />
        </div>

        <button class="btn btn-primary" type="submit">Incluir</button>
    </form>

    <div class="text-center">
        <button class="btn btn-primary text-center" type="button">
            Importar Negociações
        </button>
        <button class="btn btn-primary text-center" type="button">
            Apagar
        </button>
    </div>
    <br>
    <br>

    <div id="NegociacoesView"></div>
    <script src="js/Negociacao.js"></script>
    <script src="js/NegociacaoController.js"></script>
    <script src="js/DateHelper.js"></script>
    <script src="js/listaNegociacoes.js"></script>
    <script src="js/NegociacoesView.js"></script>
    <script>
    let negociacaoController = new NegociacaoController();
    </script>

</body>
</html>
solução!

Bom dia, Henrique! Como vai?

Pelo que vi o problema está no fato de vc ter dado o id NegociacoesView pra sua div no HTML mas no momento de recuperar ela no JS vc faz $('#negocicoesView'). O ideal é fazer como visto a seguir.

No HTML

<div id="negociacoesView"></div>

No JS

this._negociacoesView = new NegociacoesView($('#negociacoesView'));

Veja se dessa forma as coisas funcionam da forma como vc esperava.

Qualquer coisa é só falar!

Grande abraço e bons estudos, meu aluno!

acabei de testar aqui mas continua dando o mesmo erro no console acusando que o erro esta no innerHTML

bom copiando a sua linha de código aparentemente funcionou não entendo muito bem o porque pois a minha estava igual.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software