我有一个与Firebase连接的有角度的应用程序。到目前为止,我已经完成了数据库和身份验证。但是现在,我只能使用云功能。 每当有人预订受益人时,我都会尝试通过nodemailer发送电子邮件。 该功能代码大部分是从firebase github函数示例复制的 像这样:
'use strict';
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Sends an email confirmation when a user changes his mailing list subscription.
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite(async (change) => {
const snapshot = change.after;
const val = snapshot.val();
if (!snapshot.changed('subscribedToMailingList')) {
return null;
}
const mailOptions = {
from: '"Spammy Corp." <noreply@firebase.com>',
to: val.email,
};
const subscribed = val.subscribedToMailingList;
// Building Email message.
mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
mailOptions.text = subscribed ?
'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' :
'I hereby confirm that I will stop sending you the newsletter.';
try {
await mailTransport.sendMail(mailOptions);
console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email);
} catch(error) {
console.error('There was an error while sending the email:', error);
}
return null;
});
此后,我将环境变量设置如下:
firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"
我在使用firebase服务时将我的功能部署到firebase时出现错误: -TypeError:无法读取未定义的属性“电子邮件”
当我检查firebase函数时:config:得到它向我显示正确的数据 该Webapp本身尚未部署(可以吗?)
任何想法/帮助将不胜感激
答案 0 :(得分:2)
如果您想使用firebase serve
在本地进行功能仿真以获取环境变量,则需要遵循documentation中的以下说明:
如果您使用的是自定义函数配置变量,请运行 在项目的功能目录中的以下命令之前 运行Firebase服务。
firebase functions:config:get > .runtimeconfig.json
但是,如果您使用的是Windows PowerShell,请替换上面的命令 与:
firebase functions:config:get | ac .runtimeconfig.json
答案 1 :(得分:0)
为什么不使用.env
目录中的functions
文件?它会自动捕获环境变量。