node.js - 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);
}
解决方案
我相信这个错误来自这一行:
recaptcha: this.recaptcha.render(),
这是因为 this.recaptcha 为空。可能,这段代码是从其他地方复制的,这里定义了 this.recaptcha。
推荐阅读
- javascript - 在 node.js 中使用 express “无法获取 /”
- javascript - 在页面初始化时获取图像
- python - Django FloatField 无法正确显示:54 而不是 5.4(Postgresql 中的数据存储)
- python - 拟合多条指数曲线(TypeError: Improper input: N=2 must not超过 M=1)
- csv - 仅在非数字字段中生成带有引号的 CSV 文件 (Apache Camel BindyCSVDataFormat)
- android - 颤振 - 对齐底部没有到达底部
- javascript - 如何将查询发送到另一个网站页面并找到我可以导入到我的网站的 .length 值
- c - 如何在 Linux 中安装库
- dax - DAX Measure - 将数字输出为文本类型
- debugging - 调试 VSIX(Visual Studio 扩展)时出现问题