我正在尝试实现一个函数,如果有数据写入我的firebase实时数据库,它将向我发送电子邮件。但是,由于错误声明“未定义mailTransport”(见下文),电子邮件永远不会被发送,
这是我的代码,我从stackoverflow上的另一篇文章中得到了这些代码。
// Function to send email
exports.sendTestEmail = functions.database.ref('/messages')
.onWrite(event => {
return sendTestEmail('testemail@gmail.com');
})
// Sends a notification to me
function sendTestEmail(email) {
const mailOptions = {
from: `someone`,
to: email
};
mailOptions.subject = `Welcome!`;
mailOptions.text = `Hey there! Here is the email notification`;
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New message sent!);
return
});
}
但是,我认为这段代码是正确的,问题是mailTransport无法识别。我确实通过我的终端安装了nodemailer,但这没有任何区别。那么,简而言之,我如何让我的代码识别mailTransport?
以下是错误的日志报告:
ReferenceError: mailTransport is not defined
at sendTestEmail (/user_code/index.js:32:10)
at exports.sendTestEmail.functions.database.ref.onWrite.event (/user_code/index.js:16:12)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:695:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
提前多多感谢!
答案 0 :(得分:2)
您似乎忘了将nodemailer
导入到脚本中。在index.js
添加
const nodemailer = require('nodemailer');
请注意,我是在https://nodemailer.com/about/#example上的第一个nodemailer示例中得到的,所以如果您遇到问题,我建议您花一些时间。