未部署电子邮件触发器的Firebase云函数(Node.js)

未部署电子邮件触发器的Firebase云函数(Node.js),node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,我一直在部署我的第一个Firebase云功能。我得到以下错误: Functions deploy had errors with the following functions: onDataAdded To try redeploying those functions, run: firebase deploy --only "functions:onDataAdded" To continue deploying other features (such a

我一直在部署我的第一个Firebase云功能。我得到以下错误:

Functions deploy had errors with the following functions:
    onDataAdded

To try redeploying those functions, run:
firebase deploy --only "functions:onDataAdded"

To continue deploying other features (such as database), run:
firebase deploy --except functions

Error: Functions did not deploy properly.
这是我的代码:

const functions = require('firebase-functions');
const nodemailer = require("nodemailer")
const mg = require("nodemailer-mailgun-transport")
const handlebars = require("handlebars")
const fs = require("fs")
const path = require("path")

const emailTemplateSource = fs.readFileSync(path.join(__dirname,"/template.hbs"), "utf8")

const mailgunAuth = {
  auth: {
    api_key: "12345",
    domain: "domain"
  }
}

const smtpTransport = nodemailer.createTransport(mg(mailgunAuth))

const template = handlebars.compile(emailTemplateSource)

const htmlToSend = template({message: "Hello!"});

const mailOptions = {
  from: "demo@demo.com",
  to: "demo@demo1.com",
  subject: "Completed",
  html: htmlToSend
};

exports.onDataAdded=functions.database.ref('/Reservation/{id}').onCreate( e=> 
{
    console.log("Reservation Complete");
    const data= e.val();
    const newData= {
    email: data.email.toUpperCase()
    };
    
    smtpTransport.sendMail(mailOptions, function(error, response) {
    if (error) {
      console.log(error)
    } else {
      console.log("Successfully sent email.")
    }
  });


    return e.ref.parent.child('newData').set(newData);
} );
我尝试在没有函数的情况下部署它,但它已部署。我不知道到底是什么导致了这个错误

p.S.我替换了API密钥和域名


编辑:更改了代码并在函数中调用了smpt.transport

一般来说,您似乎在该代码的全局范围内做了太多的工作,尤其是调用
smtpTransport.sendMail
。完全不清楚为什么在调用该函数之前需要发送电子邮件


将代码移动到函数调用中,使其仅在调用函数时运行。此外,在测试时,尽量减少总体部署的代码量,以便缩小问题的范围。

Hi。在调用函数之前,我错误地放置了smtpTransport.sendMail部分。我修复了它,但问题仍然存在。您可能也不应该读取全局范围内的文件。这是package.JSON文件的问题。我不得不手动添加依赖项。现在它起作用了。