gpt4 book ai didi

javascript - Firebase 函数环境变量无法读取未定义的属性

转载 作者:行者123 更新时间:2023-12-03 18:37:35 25 4
gpt4 key购买 nike

我有一个与 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 本身尚未部署(可能是这样吗?)

任何想法/帮助将不胜感激
谢谢

最佳答案

如果您希望使用 firebase serve 对函数进行本地模拟要获取环境变量,您需要遵循 documentation 中的这条指令:

If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.

firebase functions:config:get > .runtimeconfig.json

However, if you're using Windows PowerShell, replace the above command with:

firebase functions:config:get | ac .runtimeconfig.json

关于javascript - Firebase 函数环境变量无法读取未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51883178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com