Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
TypeError: Cannot read properties of undefined (reading 'map') ReactChartjs

This is the code:

import React from 'react';
import {connect} from 'react-redux';
import {vote} from '../store/actions';
import BarChart from "../services/BarChart";
import {color} from '../services/color'


const Poll = ({poll, vote}) => {

    const answers = poll.candidates && poll.candidates.map(candidate => (
        <button onClick={() => vote(poll._id, {answer: candidate.candidate})} key={candidate._id}> {candidate.candidate} </button>
    ));

   const data = {
        labels: poll.candidates.map(candidate => candidate.candidate),
        datasets: [
            {
                label: poll.position,
                backgroundColor: poll.candidates.map(candidate => color()),
                borderColor: '#323643',
                data: poll.candidates.map(candidate => candidate.votes)
            }
        ]
   }
    

        
    return <div>
        <h3>{poll.position}</h3>
        <div>{answers}</div>
        <BarChart chartData={data} />
    </div>
};


export default connect(store => ({
    poll: store.currentPoll
}),{vote},)(Poll);

I'm trying to graph a chart using data maping. I don't know what should be the correct syntax here. Image error link below.

Error:

Browser Error Console Error

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This means that poll.candidates is not an array. You can add optional chaining to prevent the error such as poll?.candidates?.map(...), but that won't render your information. You need to find why candidates is undefined.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!