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

138
Visualizações
vanilla js error "cannot set property of undefined" on the chrome console

Hey I'm new to chartjs and js in general and keep getting the error ""cannot set property of undefined" on the chrome console

const dataPie = {

    labels: ['Good Packs', 'Bad Packs'],
    datasets: [{
        label: 'My First Dataset',
        data: [0.1, 0.1],
        backgroundColor: ['#00bf90', 'rgb(255, 75, 75)'],
        hoverOffset: 0
    }]
};

const configPie = {
    type: 'pie',
    data: dataPie,
    options: {}
};

const pieChart = new Chart(
    document.getElementById('chart-pie'),
    configPie
);

then in a timed loop I'm calling this and getting the error "cannot set property of undefined" when trying to do the below operation (I'm not setting the value in html and it is only set when building the pie-chart at the top).

pieChart.data.datasets.data[0] = 0.5;

like I said I'm very new to js so if anyone can help that would be great!

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

0

It looks like you were accessing the data wrong, I will include a link to the documentation. I have modified your snippet below (I don't have a timer, but I have the code changing). Make sure you call update after changing the value so it will reflect in the HTML.

datasets is an array, so you need to access the index of the dataset you wish to modify. In your case, it is [0] as you only have one dataset.

ChartJs Documentation: https://www.chartjs.org/docs/latest/developers/api.html

const dataPie = {

    labels: ['Good Packs', 'Bad Packs'],
    datasets: [{
        label: 'My First Dataset',
        data: [0.1, 0.1],
        backgroundColor: ['#00bf90', 'rgb(255, 75, 75)'],
        hoverOffset: 0
    }]
};

const configPie = {
    type: 'pie',
    data: dataPie,
    options: {}
};

const pieChart = new Chart(
    document.getElementById('chart-pie').getContext('2d'),
    configPie
);

pieChart.data.datasets[0].data[0] = 0.5;
pieChart.update('active');
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.7.1/dist/chart.min.js"></script>
<canvas id="chart-pie" width="400" height="400"></canvas>

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