Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
Cannot read properties of undefined (reading 'DateTime') even I check there is a data in React.js

I'm just getting learning React a few weeks ago. So, If I ask a pretty general question, I am sorry. But, It would be really appreciated if you help me out!

  • Issue : Cannot read properties of undefined (reading 'DateTime')

  • What I try to solve it : Firstly, I tried to solve it generally. I mean I add the condition to progress my code and re-render. Also, I check the data exist or not.

  • Code

  const changeWeekendDateColor = () => {
    let eventArray = [];
    let weekendArray = [];
    if (weekendData && weekendData.event && weekendData.schedule) {
      let status = false;
      const dataEventLength = weekendData.event.length;
      const dataWeekendLength = weekendData.schedule.length;

      for (let i = 0; i <= dataEventLength; i++) {
        eventArray.push(weekendData.event[i].DateTime.split("T")[0]);
      }

      for (let i = 0; i <= dataWeekendLength; i++) {
        weekendArray.push(weekendData.schedule[i].split(" ")[0]);
      }
    }

    for (let i = 0; i <= eventArray.length; i++) {
      if (weekendArray.includes(eventArray[i])) return (status = false);
    }

    return status;
  };
  console.log(changeWeekendDateColor(weekendData));

WeekendData is this one

enter image description here

  • What I expect : I add if statement
if (weekendData && weekendData.event && weekendData.schedule)

So, I think this should be work because if there is no data, it shouldn't do the next line. I am not sure why my code couldn't approach the weekendData.event.DateTime...

enter image description here

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

for loop can't read undefiened property. dataWeekendLength has 7 length but weekendData.event only have 1 length from the array.

See WeekendData.schedule.length

...
schedule: Array(7)

And this is for loop in the code,

      for (let i = 0; i <= dataEventLength; i++) {
        eventArray.push(weekendData.event[i].DateTime.split("T")[0]);
      }

The for loops 7 times for the dataEventLength,

It's weekendData.event,

...
event: Array(1)

If the for loop goes on i = 1 then it will be weekendData.event[1].

But there is no weekendData.event[1] but only weekendData.event[0].

The code would work but the array only has one.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda