问题描述
所以我在编码方面还很陌生,并且一直在与Node一起玩,所以我认为我会完成一个教程,而在这一节中我很难过。
目标是对如果忘记密码将发送密码重置链接的路由进行编码。我们使用的是用于模板的把手和用于发送电子邮件的nodemailer。以下是我到目前为止在此特定路由文件中的代码:
const express = require('express');
const router = express.Router();
const hbs = require('express-handlebars');
const nodemailer = require('nodemailer');
const path = require('path');
const email = process.env.EMAIL;
const password = process.env.EMAIL_PASSWORD;
const smtpTransport = nodemailer.createTransport({
service: process.env.EMAIL_PROVIDER,auth: {
email: email,password: password
}
})
const handlebarsOptions = {
viewEngine: {
extName: '.hbs',defaultLayout: null,partialsDir: './templates/',layoutsDir: './templates/'
},viewPath: path.resolve('./templates/'),extName: '.html',};
smtpTransport.use('compile',hbs(handlebarsOptions));
router.post('/forgot-password',async (request,response) => {
console.log('called forgot-password');
if(!request.body || !request.body.email){
response.status(400).json({message: "invalid body",status: 400});
} else {
console.log('email exists');
const userEmail = request.body.email;
console.log('email set');
const emailOptions = {
to: userEmail,from: email,template: 'forgotPassword',subject: 'MMO Password Reset',context: {
name: 'joe',url: `http://localhost:${process.env.PORT || 3000}`
}
}
console.log('options applied');
await smtpTransport.sendMail(emailOptions)
console.log('email sent');
response.status(200).json({message: `email has been sent to ${userEmail} with instructions`,status: 200});
}
});
module.exports = router;
这是我测试路由时在控制台中得到的:
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
connected to mongo
server is running on port: 3000
called forgot-password
email exists
email set
options applied
(node:4838) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at validateString (internal/validators.js:113:11)
at Object.resolve (path.js:981:7)
at ExpressHandlebars.getTemplate (/home/tobias/api/node_modules/express-handlebars/lib/express-handlebars.js:106:18)
at ExpressHandlebars.render (/home/tobias/api/node_modules/express-handlebars/lib/express-handlebars.js:156:8)
at ExpressHandlebars.renderView (/home/tobias/api/node_modules/express-handlebars/lib/express-handlebars.js:234:7)
at processplugins (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
at /home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:283:17
at Mail._convertDataimages (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:387:20)
at Mail._defaultPlugins.compile (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:31:41)
at processplugins (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
at Mail._processplugins (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:287:9)
at Mail.sendMail (/home/tobias/api/node_modules/nodemailer/lib/mailer/index.js:164:14)
at /home/tobias/api/routes/password.js:53:25
at Layer.handle [as handle_request] (/home/tobias/api/node_modules/express/lib/router/layer.js:95:5)
at next (/home/tobias/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/tobias/api/node_modules/express/lib/router/route.js:112:3)
(node:4838) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:4838) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我检查了环境变量,重新启动了nodemon,并检查以确保forgotPassword.html实际上是./templates中的文件,并确保已安装所有必需的软件包等。我的代码看起来像是教程中的代码(除了我为了确保错误发生在我所认为的位置而输入的console.logs之外)。我有点受阻,对我似乎无法弄清楚这一点有些沮丧。帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)