【问题标题】:Firebase Cloud function mailTransport is not definedFirebase Cloud 函数 mailTransport 未定义
【发布时间】:2018-02-16 19:14:58
【问题描述】:

我正在尝试实现一个功能,如果有数据写入我的 firebase 实时数据库,它将向我发送电子邮件。但是,由于“未定义邮件传输”的错误(见下文),电子邮件永远不会被发送,

这是我拥有的代码,它是从 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)

提前非常感谢!

【问题讨论】:

  • 在你借用的那个代码中,当然是在那个代码的某个地方,mailTransport 变量被定义和赋值了吗?如果没有,你必须自己做。

标签: node.js firebase google-cloud-functions nodemailer


【解决方案1】:

您似乎忘记将nodemailer 导入脚本。在index.js顶部添加

const nodemailer = require('nodemailer');

请注意,我是从 https://nodemailer.com/about/#example 上的第一个 nodemailer 示例中获得的,所以如果您有问题,我建议您花一些时间在那里。

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2021-09-19
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多