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

349
Vistas
TypeError: Cannot read property 'variable' of undefined

I am getting this undefined error in my node js written program. This was the line that was causing the issue

sockets[index].notification = 1;

So in order to resolve this, I did this

if ( typeof sockets[index].notification !== 'undefined' ) {
                              
   sockets[index].notification = 1;
 }

But it still gave the same error

if ( typeof sockets[index].notification_acc_on !== 'undefined' ) {
                                                   ^

TypeError: Cannot read property 'notification' of undefined
    at Socket.<anonymous> (/var/www/html/server.js:506:56)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)

How can I check and handle this so if the variable isn't set, my program doesn't crash at least? In short I want to prevent the crash

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It appears that the problem is with sockets[index] being undefined.

if ( typeof sockets[index] !== 'undefined' ) {
                              
   sockets[index].notification = 1;
}

There are other checks you might consider exploring, for example checking for null value, confirming that sockets exists, etc. My response only deals with the immediate problem from the original post.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You reveive this error because either sockets or sockets[index] is undefined. To avoid this error, you can perform the following check:

if (typeof sockets?.[index] !== 'undefined') {
}
about 4 years ago · Juan Pablo Isaza 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