无法调用方法'渲染'未定义的

时间:2015-06-19 13:34:23

标签: javascript

我创造了"员工"数组,并希望使用该数组数据生成邮件。我在Node.js中创建了以下功能。

我还创建了一个名为以下功能的cron作业。调用此函数时,会引发以下错误:
Cannot call method 'render' of undefined

exports.missedWorksheets = function(req, res){

  var user = new User();

  var dailyCurrentDate = [];
  var dailyCurrentDate1 = [];
  var missedWorkByUser = [];
  var employees = [];
  var currentDate = moment(Date.now()).format('YYYY-MM-DD');
  console.log(currentDate);
  var lastDate = moment(Date.now()).subtract(7,'days');
  var addedDate = moment(lastDate).format('YYYY-MM-DD');
  console.log(addedDate);
  for(var i=0; i<5; i++)
  {
    dailyCurrentDate[i] = moment(addedDate).format('YYYY-MM-DD');
    dailyCurrentDate[i] = moment(dailyCurrentDate[i]).add(1,'days');
    dailyCurrentDate1[i] = moment(dailyCurrentDate[i]).format('YYYY-MM-DD');
    addedDate = moment(dailyCurrentDate1[i]).format('YYYY-MM-DD');
    console.log(dailyCurrentDate1[i]);
  }

  User.find().exec(function(err, missedUsers) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      Worksheet.find().populate('employee','._id').exec(function(err, missedWorksheets){
        if (err) {
          return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
          });
        }else{
          for(var i=0; i<missedUsers.length; i++){
            for(var j=0; j<missedWorksheets.length; j++){
              if(String(missedUsers[i]._id) != String(missedWorksheets[j].employee._id))
              {
                employees[i] = missedUsers[i];
              }
            }
          }
          async.waterfall([
                          function(done)
                          {
                            console.log("Async called.");
                            done(err,employees);
                          },
                          function(employees,done) {
                            console.log("Template fetch called.");
                            app.render('templates/weekly-worksheet-checking', {
                  //user: user,
                  employee: employees,
                }, function(err,emailHTML) {
                  done(err,emailHTML,employees);
                });
                          },
              // If valid email, send reset email using service
              function(emailHTML,employees, done) {
                console.log("1-mail called.");
                var mailToEmployee = config.mailer.from;
                var smtpTransport = nodemailer.createTransport(config.mailer.options);
                //users.getActiveAdminUsers(function(activeAdminUsers){
                  var receivers=mailToEmployee+',';
                // for (var i in activeAdminUsers){
                //  var activeAdminUser =   activeAdminUsers[i];
                //  receivers = receivers + activeAdminUser.email+',';
                // }
                var mailOptions = {
                  to: 'hareshr@systemintegration.in',
                  from: config.mailer.from,
                  subject: 'Unfill WorksheetList of Previous year',
                  html: emailHTML
                };
                smtpTransport.sendMail(mailOptions, function(err) {
                  if (!err) {
                    //  Notification.email = leaveResponse.employee.email;
                    //  Notification.summary = leaveResponse.leaveScheduleType;
                    //  notifications.approveLeaveNotification(req,res,Notification.email,Notification.summary);
                    res.jsonp(employees);
                  }

                  done(err);
                });
                //});
}
],function(err) {
  if (err){
    console.log(err);
    return res.send({
     message: errorHandler.getErrorMessage(err)
   });
  }
});
}
});

}
});
};

1 个答案:

答案 0 :(得分:0)

错误说明了一切,您的app对象未定义。 由于您似乎在回调函数中调用该方法,因此可能在JS代码的另一个范围中定义app对象。

查看bind方法。

<强>文档
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

有关如何使用它的更多详细信息:
http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/#JavaScript8217s_Bind_Method

  

换句话说,bind()允许我们轻松设置哪个特定对象   在调用函数或方法时将绑定到此。