使用 Nodemailer 时如何修复 "await is only valid in async function" 错误
How to fix "await is only valid in async function" error when using Nodemailer
我正在尝试按照教程使用 Nodemailer 创建联系我们表单。本教程从Nodemailer网站上粘贴了一些代码,但是自从录制该教程以来,源代码已更新。
我粘贴了网站上的最新代码,但出现以下控制台错误:
let info = await transporter.sendMail(mailOptions)
SyntaxError: await is only valid in async function
下面是我的 app.js 代码:
const express = require("express");
const bodyParser = require("body-parser");
const exphbs = require("express-handlebars");
const path = require("path");
const nodemailer = require("nodemailer");
const app = express();
// View engine setup
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
// Static folder
app.use("/public", express.static(path.join(__dirname, "public")));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.render("contact");
});
app.post("/send", (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Company: ${req.body.company}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@address.com',
pass: 'yourpassword'
}
});
const mailOptions = {
from: 'sender@email.com', // sender address
to: 'to@email.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Your html here</p>'// plain text body
};
let info = await transporter.sendMail(mailOptions)
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
app.listen(3000, () => console.log("Server started..."));
我粘贴的代码位于此页面:https://nodemailer.com/about/
为什么我会收到此错误,我需要进行哪些更改才能使代码按预期工作?
那是因为await
只在async
函数内部有效。语法是
async function some() {
await other
}
更改此行:
app.post("/send", (req, res) => {
到
注意这里的async
关键字
app.post("/send", async (req, res) => {
阅读更多关于 async/await
transporter.sendMail(mailOptions)
是异步操作,因此该示例在它之前使用 await
关键字以使执行同步。
但是,await 关键字只能在标记为 async
的方法中使用。因此,在您的情况下,您需要将 createTransport
方法标记为异步。
例如:
async nodemail.createTransport
另一种选择是从方法中返回 transport.sendMail()
并解决承诺。
例如:
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@address.com',
pass: 'yourpassword'
}
});
const mailOptions = {
from: 'sender@email.com', // sender address
to: 'to@email.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Your html here</p>'// plain text body
};
return transporter.sendMail(mailOptions)
});
transport.then(
result => {
console.log("Message sent: %s", result.info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(result.info));
}
);
如果不使函数异步,则不能使用 await。所以,只需将异步放在 (req, res)
之前,如下所示。
app.post("/send", async (req, res) => {
//other stuff will goes here.
let info = await transporter.sendMail(mailOptions)
});
await
只能在 async
函数中使用。
需要注意的是async
函数仅用于使 promise 以更舒适的方式工作。
所以只需像这样添加 async
app.post("/send", async (req, res) =>
或从
中删除 let info = await
`let info = await transporter.sendMail(mailOptions)
而是像这样使用 then
链
transporter.sendMail(mailOptions).then((info) => { your code})
我正在尝试按照教程使用 Nodemailer 创建联系我们表单。本教程从Nodemailer网站上粘贴了一些代码,但是自从录制该教程以来,源代码已更新。
我粘贴了网站上的最新代码,但出现以下控制台错误:
let info = await transporter.sendMail(mailOptions)
SyntaxError: await is only valid in async function
下面是我的 app.js 代码:
const express = require("express");
const bodyParser = require("body-parser");
const exphbs = require("express-handlebars");
const path = require("path");
const nodemailer = require("nodemailer");
const app = express();
// View engine setup
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
// Static folder
app.use("/public", express.static(path.join(__dirname, "public")));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.render("contact");
});
app.post("/send", (req, res) => {
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Company: ${req.body.company}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@address.com',
pass: 'yourpassword'
}
});
const mailOptions = {
from: 'sender@email.com', // sender address
to: 'to@email.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Your html here</p>'// plain text body
};
let info = await transporter.sendMail(mailOptions)
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
app.listen(3000, () => console.log("Server started..."));
我粘贴的代码位于此页面:https://nodemailer.com/about/
为什么我会收到此错误,我需要进行哪些更改才能使代码按预期工作?
那是因为await
只在async
函数内部有效。语法是
async function some() {
await other
}
更改此行:
app.post("/send", (req, res) => {
到
注意这里的async
关键字
app.post("/send", async (req, res) => {
阅读更多关于 async/await
transporter.sendMail(mailOptions)
是异步操作,因此该示例在它之前使用 await
关键字以使执行同步。
但是,await 关键字只能在标记为 async
的方法中使用。因此,在您的情况下,您需要将 createTransport
方法标记为异步。
例如:
async nodemail.createTransport
另一种选择是从方法中返回 transport.sendMail()
并解决承诺。
例如:
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@address.com',
pass: 'yourpassword'
}
});
const mailOptions = {
from: 'sender@email.com', // sender address
to: 'to@email.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Your html here</p>'// plain text body
};
return transporter.sendMail(mailOptions)
});
transport.then(
result => {
console.log("Message sent: %s", result.info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(result.info));
}
);
如果不使函数异步,则不能使用 await。所以,只需将异步放在 (req, res)
之前,如下所示。
app.post("/send", async (req, res) => {
//other stuff will goes here.
let info = await transporter.sendMail(mailOptions)
});
await
只能在 async
函数中使用。
需要注意的是async
函数仅用于使 promise 以更舒适的方式工作。
所以只需像这样添加 async
app.post("/send", async (req, res) =>
或从
let info = await
`let info = await transporter.sendMail(mailOptions)
而是像这样使用 then
链
transporter.sendMail(mailOptions).then((info) => { your code})