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

463
Vistas
Uncaught TypeError: Cannot convert undefined or null to object React

I'm having a strange problem with my code. There is an object that comes from an API call where videos are stored and I need to add the number of videos on the website. But sometimes there can be "null" coming from API call and obviously, I don't have to show the length of the null value.

As I wrote this code which works when I try on editors, but when I use it on my project it gives an error Cannot convert undefined or null to object. Can anyone give me an idea or hint of what I'm doing wrong? And sorry if I explained unclearly, this is my very first post here. Thank you!

const object = {
  background_video: null,
  vimeo_branded: 'video url',
  vimeo_unbranded: "video url"
}

const removeFalsyElement = object => {
  let sum = 0;
  Object.keys(object).forEach(key => {
    if (object[key]) {
      sum += 1;
    }
  });
  return sum;
};

console.log(removeFalsyElement(object)) // output should be 2.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The Object.keys method expects an object always. if we pass null we would get the above mentioned error

const removeFalsyElement = (object = {}) => {//default value for object in case it is null. Object.keys needs an object always


let sum = 0;
  Object.keys(object).forEach(key => {
    if (object[key]) {
      sum += 1;
    }
  });
  return sum;
};
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