Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Cannot read property 'getDate' of undefined

Bom dia, estou com um erro ao tentar adicionar os elementos inseridos na tabela. Aparece o seguinte erro:

DateHelper.js:8 Uncaught TypeError: Cannot read property 'getDate' of undefined at Function.dataParaTexto (DateHelper.js:8) at NegociacoesView.js:23 at Array.map () at NegociacoesView._template (NegociacoesView.js:20) at NegociacoesView.update (NegociacoesView.js:39) at NegociacaoController.adiciona (NegociacaoController.js:16) at HTMLFormElement.onsubmit (index.html:14)

Já dei uma olhada em outros erros semelhante no fórum, mas nenhuma das resoluções resolveram!

segue meus códigos:

class NegociacoesView{

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

    _template(model){
        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>
                ${model.negociacoes.map( n => {
                    return ` 
                        <tr>
                            <td>${DateHelper.dataParaTexto(n.data)}</td>
                            <td>${n.quantidade}</td>
                            <td>${n.valor}</td>
                            <td>${n.volume}</td>
                        </tr>
                    `
                }).join(' ')}
            </tbody>

            <tfoot>
            </tfoot>
        </table>
        `;
    }

    update(model){
        this._elemento.innerHTML = this._template(model);
    }
}
class DateHelper{

    constructor(){
        throw new Error('DateHelper não pode ser instanciada');
    }

   static dataParaTexto(data){
        return `${data.getDate()}/${data.getMonth()+1}/${data.getFullYear()}`;
    }

    static textoParaData(texto){
        if(!/\d{4}-\d{2}-\d{2}/.test(texto)) throw new Error('Deve estar no formato aaaa-mm-dd');
        return new Date(...texto.split('-').map((item, indice) => item - indice % 2));
    }

}
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($('#negociacoesView'));
        this._negociacoesView.update(this._listaNegociacoes);
    }

    adiciona(event) {
        event.preventDefault();
        this._listaNegociacoes.adiciona(this._criaNegociacao());
        this._negociacoesView.update(this._listaNegociacoes);
        this._limpaFormulario();
    }
    _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();    
    }
}
class Negociacao{

    constructor(data, quantidade, valor){
        this._data = new Date(data.getTime());
        this._quantidade = quantidade;
        this._valor= valor;
        Object.freeze(this);
    }

    get volume(){
        return this._quantidade * this._valor;
    }

    get data(){
        return new Date(this._data.getTime());
    }

    get quantidade(){
        return this._quantidade;
    }

    get valor(){
        return this._valor;
    }

}

Se precisar de mais alguma parte, só comentar!

Aguardo retorno e agradeço desde já.

1 resposta
solução!

O erro estava na linha

adiciona(negociacao){ this._negociacoes.push(negociacao); }

Estava da seguinte maneira: adiciona(negociacoes){ this._negociacoes.push(Negociacao); }

class ListaNegociacoes{

    constructor(){

        this._negociacoes = [];
    }

    adiciona(negociacao){
        this._negociacoes.push(negociacao);
    }

    get negociacoes(){
        return  [].concat(this._negociacoes);
    }
}