Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda