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

193
Vistas
how can i avoid error "can't convert undefined to an object "

some of the document does not consists planDetails planId properties and its returning error "can't convert undefined to an object , but i have to fetch that document if these properties exists or not , how can i avoid this error

  this.profile.find({ userId: ObjectId(req.id) }).populate("planId").lean().exec(function(err, profileFound) {
  console.log(profileFound);
  if (err) return callback(err);
  if (profileFound && profileFound.length === 0) {
    return callback(false, common.errorMsg("PROFILE DOES NOT EXIST"));
  } else {
    var profileData = profileFound[0];
    profileData["isPlanTaken"] = false;
    profileData["planDetails"] = {};
    if(profileData.hasOwnProperty("planId") && profileData.planId){
      if(Object.keys(profileData.planId).length>0){
     profileData["isPlanTaken"]=true;
      profileData["planDetails"]=profileData.planId;
    }
  }

    return callback(
      false,
      common.successMsg("PROFILE FETCHED",profileData )
    );
  }
});
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

I think there was missing the initialization of profileData variable only...

const profileData = {};
profileData["isPlanTaken"] = false;

console.log(profileData);

profileData["planDetails"] = {};

if(profileData.hasOwnProperty("planId") && Object.keys(profileData.planId)){
  profileData["isPlanTaken"]=true;
  profileData["planDetails"]=profileData.planId;
}

console.log(profileData);

And I think that is also enough to check a property is exists in an object. Can you please give me more data for example, because I don't really know what do you need.

const profileData = {};
profileData.isPlanTaken = false;

console.log(profileData);

profileData.planId = 5; // for example

if (profileData.planId) {
  profileData.isPlanTaken = true;
  profileData.planDetails = profileData.planId;
}

console.log(profileData);

about 4 years ago · Santiago Gelvez 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