title: how can i avoid error "can't convert undefined to an object "
body: <p>some of the document does not consists planDetails planId properties and its returning error &quot;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</p>
  <pre><code>  this.profile.find({ userId: ObjectId(req.id) }).populate(&quot;planId&quot;).lean().exec(function(err, profileFound) {
    console.log(profileFound);
    if (err) return callback(err);
    if (profileFound &amp;&amp; profileFound.length === 0) {
      return callback(false, common.errorMsg(&quot;PROFILE DOES NOT EXIST&quot;));
    } else {
      var profileData = profileFound[0];
      profileData[&quot;isPlanTaken&quot;] = false;
      profileData[&quot;planDetails&quot;] = {};
      if(profileData.hasOwnProperty(&quot;planId&quot;) &amp;&amp; profileData.planId){
        if(Object.keys(profileData.planId).length&gt;0){
       profileData[&quot;isPlanTaken&quot;]=true;
        profileData[&quot;planDetails&quot;]=profileData.planId;
      }
    }
  
      return callback(
        false,
        common.successMsg(&quot;PROFILE FETCHED&quot;,profileData )
      );
    }
  });
  </code></pre>