首页 > 解决方案 > Express.js - TypeError:无法读取未定义的属性“渲染”

问题描述

在我的 Express.js 后端,当用户想要重置密码时,会通过 Sendgrid 将链接发送到他们的电子邮件,当单击该链接时,用户会收到此错误:

TypeError: Cannot read property 'render' of undefined
    at resetPasswordController.showResetPassword (/root/apps/AskArya-Node.js-Vue.js/Server/app/http/controllers/auth/resetPasswordController.js:11:33)
    at Layer.handle [as handle_request] (/root/apps/AskArya-Node.js-Vue.js/Server/node_modules/express/lib/router/layer.js:95:5)
    at next (/root/apps/AskArya-Node.js-

resetPasswordController 的代码是:

  class resetPasswordController extends controller {
  showResetPassword(req, res) {
    const title = 'بازیابی رمز عبور';
    res.render('home/auth/passwords/reset', {
      recaptcha: this.recaptcha.render(),
      title,
      token: req.params.token
    });
  }

  async resetPasswordProccess(req, res, next) {
    await this.recaptchaValidation(req, res);
    let result = await this.validationData(req);
    if (result) {
      return this.resetPassword(req, res);
    }

    this.back(req, res);
  }

  async resetPassword(req, res) {
    let field = await PasswordReset.findOne({
      $and: [{ email: req.body.email }, { token: req.body.token }]
    });
    if (!field) {
      req.flash('errors', 'اطلاعات وارد شده صحیح نیست لطفا دقت کنید');
      return this.back(req, res);
    }

标签: node.jsapiexpress

解决方案


我相信这个错误来自这一行:

recaptcha: this.recaptcha.render(),

这是因为 this.recaptcha 为空。可能,这段代码是从其他地方复制的,这里定义了 this.recaptcha。


推荐阅读