【发布时间】:2019-01-23 18:36:56
【问题描述】:
我有一个与 firebase 连接的 Angular 应用程序。到目前为止,我已经完成了数据库和身份验证工作。但现在我被困在云功能上。 每次有人预订受益人时,我都会尝试使用 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 serve 时将我的函数部署到 firebase,但出现错误: -TypeError:无法读取未定义的属性“电子邮件”
当我检查 firebase 函数时:config:get 它显示了正确的数据 webapp 本身尚未部署(可能是这样吗?)
任何想法/帮助将不胜感激 谢谢
【问题讨论】:
标签: javascript angular firebase google-cloud-functions