Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!