Please help what is going on here

Cannot read properties of undefined (reading ‘courseCertificate’)
there is a courseCertificate field already contain the data but still not reading it

getCourseDetails(language, req, callback) {                                            //get course details
    console.log(req.id);
    let that = this;
    this.course
      .find(
        { _id: ObjectId(req.query.courseId), companyId: req.query.companyId },
        { __v: 0, createdBy: 0, createdAt: 0, updatedAt: 0, isDelete: 0 }
      )
      .populate("courseDetail.interestId")
      .populate("courseCertificate.certificateId")
      .populate("courseCertificate.trainers")
      .lean()
      .exec(function (err, courseFound) {
        console.log("------------------", courseFound)
        that.user.findOne({ _id: req.id }, function (err, user) {
          console.log("------------------------", user.fullName);
          if (err) return callback(err);
          if (courseFound && courseFound.length === 0) {
            return callback(false, common.errorMsg("COURSE DOES NOT EXIST"));
          } else {
            that.courseRegister
              .find({
                userId: ObjectId(req.id),
                courseId: ObjectId(req.query.courseId),
              })
              .lean()
              .exec(function (err, courseReg) {
                that.feedback.aggregate([
                  {
                    "$match": { courseId: ObjectId(req.query.courseId) }
                  },
                  {
                    $project: {
                      rating: "$result.likeCourse"
                    }
                  }, {
                    $group: {
                      _id: null,
                      sum: { $sum: "$rating" },
                      count: { $sum: 1 }
                    }
                  }, {
                    $project: {
                      rating: { $divide: ["$sum", "$count"] }
                    }
                  }
                ], function (err, feedback) {
                  let _preAssessmentCompleted = false;
                  let _finalAssessmentCompeleted = false;
                  let _summaryCompeleted = false;
                  if (courseReg.length > 0) {
                    _preAssessmentCompleted = courseReg[0].preAssessment;
                    _finalAssessmentCompeleted = courseReg[0].finalAssessment;
                    _summaryCompeleted = courseReg[0].courseCompeltion;
                  }

                  that.courseDetail
                    .find({ courseId: ObjectId(req.query.courseId) })
                    .lean()
                    .exec(function (err, courseDetails) {
                      courseFound[0].courseDetail.skillId = courseFound[0].courseDetail.interestId.skills.filter(
                        function (item) {
                          return (item._id =
                            courseFound[0].courseDetail.skillId);
                        }
                      );
                      delete courseFound[0].courseDetail.interestId.skills;
                      courseFound[0]["summary"] =
                        courseDetails.length > 0
                          ? _.map(courseDetails[0].sections, function (item) {
                            item["isCompleted"] = false;

                            if (
                              courseReg.length > 0 &&
                              courseReg[0].hasOwnProperty(
                                "courseAssessmentResponse"
                              )
                            ) {
                              if (courseReg[0].courseAssessmentResponse.length > 0) {

                                let sec_found = courseReg[0].courseAssessmentResponse.filter(
                                  function (it) {
                                    // console.log(it,item._id)
                                    return (
                                      it.sectionId.toString() == item._id.toString()
                                    );
                                  }
                                );
                                item["isCompleted"] = sec_found.length > 0 ? true : false;
                              }
                            }
                            // if (courseReg.length > 0 && courseReg[0].hasOwnProperty('lessons')) {

                            //     let topicLength   = 0;
                            //     _.forEach(item.lesson,function(t){
                            //        topicLength = topicLength + t.topics.length
                            //     });
                            //     let courseRegTopicLen = 0;
                            //     if(courseReg[0].lessons.length>0){
                            //     courseRegTopicLen = courseReg[0].lessons.filter(function(s){
                            //         return s.sectionId.toString() == item._id.toString()
                            //     }).length
                            //     }

                            //     console.log("==============",courseRegTopicLen, topicLength)
                            //     if (courseRegTopicLen >= topicLength) {
                            //         console.log("I am true ")
                            //         item["isCompleted"] = true;
                            //     }
                            // }

                            item.lesson = _.map(item.lesson, function (
                              items
                            ) {
                              let l_found = [];
                              if (courseReg.length > 0) {
                                if (
                                  courseReg[0].hasOwnProperty("lessons")
                                ) {
                                  l_found = courseReg[0].lessons.filter(
                                    function (l) {
                                      return (
                                        l.lessonId.toString() ==
                                        items._id.toString()
                                      );
                                    }
                                  );
                                }
                              }
                              // console.log(items.topics);
                              let t_found = [];
                              items.topics = _.map(items.topics, function (
                                topic
                              ) {
                                if (courseReg.length > 0) {
                                  if (
                                    courseReg[0].hasOwnProperty("lessons")
                                  ) {
                                    t_found = courseReg[0].lessons.filter(
                                      function (t) {
                                        return (
                                          t.topicId.toString() ==
                                          topic._id.toString()
                                        );
                                      }
                                    );
                                  }
                                }
                                topic["isCompleted"] =
                                  t_found.length > 0 ? true : false;
                                // console.log(topic)
                                return topic;
                              });

                              items["isCompleted"] =
                                l_found.length > 0 ? true : false;
                              return items;
                            });

                            return item;
                          })
                          : [];
                      if (courseFound[0].courseWelcome) {
                        courseFound[0].courseWelcome.isCompleted = false;
                        if (courseReg.length > 0) {
                          courseFound[0].courseWelcome.isCompleted = true;

                        }
                      }
                      if (courseFound[0].courseDetail)
                        courseFound[0].courseDetail.isCompleted = true;
                      if (courseFound[0].preAssessment)
                        courseFound[0].preAssessment.isCompleted = _preAssessmentCompleted;
                      if (courseFound[0].finalAssessment)
                        courseFound[0].finalAssessment.isCompleted = _finalAssessmentCompeleted;
                      if (courseFound[0].summary)
                        courseFound[0].summary.isCompleted = _summaryCompeleted;

                      if (
                        courseReg.length > 0 &&
                        courseReg[0].preAssessment
                      ) {
                        courseFound[0].preAssessment.sections = _.map(
                          courseFound[0].preAssessment.sections,
                          function (sec) {
                            let sec_found = courseReg[0].preAssessmentResponse.sections.filter(
                              function (r_sec) {
                                return (
                                  sec._id.toString() ==
                                  r_sec.sectionId.toString()
                                );
                              }
                            );
                            if (sec_found.length > 0) {
                              sec.questions = sec.question.map(function (
                                ques
                              ) {
                                let ques_found = sec_found[0].question.filter(
                                  function (r_ques) {
                                    return (
                                      ques._id.toString() ==
                                      r_ques.questionId.toString()
                                    );
                                  }
                                );
                                if (ques_found.length > 0) {
                                  ques.selected = ques_found[0].selected;
                                } else {
                                  ques.selected = null;
                                }

                                return ques;
                              });
                            }
                            return sec;
                          }
                        );
                      }

                      if (
                        courseReg.length > 0 &&
                        courseReg[0].finalAssessment
                      ) {
                        courseFound[0].finalAssessment.question = _.map(
                          courseFound[0].finalAssessment.question,
                          function (ques) {
                            let ques_found = courseReg[0].finalAssessmentResponse.question.filter(
                              function (r_ques) {
                                console.log(ques._id, r_ques.questionId);
                                return (
                                  ques._id.toString() ==
                                  r_ques.questionId.toString()
                                );
                              }
                            );
                            if (ques_found.length > 0) {
                              ques.selected = ques_found[0].selected;
                              let _correct = ques.options.filter(function(o){
                                  return o._id.toString() == ques.selected.toString()
                              })
                              if(_correct.length>0) ques.isCorrect = true
                             
                            } else {
                              ques.isCorrect = false
                              ques.selected = null;
                            }

                            return ques;
                          }
                        );
                      }
                        // ======================================================== if condition
                       if(courseFound[0].courseCertificate && courseFound[0].courseCertificate.trainers){
                      let trainer = 
                        courseFound[0].courseCertificate.tariners;
                      var htmlcontent =
                        courseFound[0].courseCertificate.certificateId
                          .content;
                      htmlcontent = htmlcontent.replace(
                        "{name}",
                        user.fullName
                      );
                      courseFound[0].courseCertificate = htmlcontent; }   //if condition 

                      courseFound[0].courseDetail.rating = feedback.length > 0 ? feedback[0].rating : 0
                      
                      let orderedData = {
                        courseDetail: courseFound[0].courseDetail,
                        courseWelcome: courseFound[0].courseWelcome,
                        preAssessment: courseFound[0].preAssessment,
                        summary: courseFound[0]["summary"],
                        finalAssessment: courseFound[0].finalAssessment,
                        feedback: {
                          isCompleted:
                          (courseReg &&courseReg.length > 0)
                              ? courseReg[0].feedback
                              : false,
                        },

                        courseCertificate: courseFound[0].courseCertificate,
                        isCourseCompleted: courseReg[0].courseCertificate,
                        trainerDetails: {
                          email: (trainer && trainer.length>0)?trainer[0].email:null,
                          mobile: (trainer && trainer.length>0)? trainer[0].mobile
                            : null,
                            name: (trainer && trainer.length>0)?trainer[0].fullName:"",
                        },
                        
                      };
                    

                      return callback(
                        false,
                        common.successMsg("COURSE FETCHED", [orderedData])
                      );
                    
                    });
                });
              });
          }
        });
      });
  }

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Please provide more details, that’s a lot of code that I have no idea what’s doing to debug

There is a object in database (MongoDB) which is an object which have two properties certificateId and trainer which is taking reference from their own database and as shown it populating from courseDetail ,
but in the last object let orderData it giving me error "cannot read the properties of undefined courseCertificate isCourseCompleted: courseReg[0].courseCertificate and same withe trainer