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



我一直在部署我的第一个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的调用。目前还不清楚为什么您需要在调用函数之前发送电子邮件

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

最新更新