最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase functions environment variables can not read property of undefined - Stack Overflow

programmeradmin2浏览0评论

i have an angular app thats connected with firebase. So far i've got the db and authentication working. But right now i'm stuck on cloud functions. I'm trying to send an email with nodemailer every time a person makes a reservation for a benefiet. The function code is mostly copied from firebase github functions examples like this:

'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 /
// 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." <[email protected]>',
    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;
});

After this i set my environment variable like this:

firebase functions:config:set gmail.email="[email protected]" gmail.password="secretpassword"

i deploy my function to firebase when using firebase serve i get an error: -TypeError: Cannot read property 'email' of undefined

when i check with firebase functions:config:get it shows me the correct data The webapp itself is not deployed yet (could this be it?)

Any ideas/help would be appreciated thx

i have an angular app thats connected with firebase. So far i've got the db and authentication working. But right now i'm stuck on cloud functions. I'm trying to send an email with nodemailer every time a person makes a reservation for a benefiet. The function code is mostly copied from firebase github functions examples like this:

'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." <[email protected]>',
    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;
});

After this i set my environment variable like this:

firebase functions:config:set gmail.email="[email protected]" gmail.password="secretpassword"

i deploy my function to firebase when using firebase serve i get an error: -TypeError: Cannot read property 'email' of undefined

when i check with firebase functions:config:get it shows me the correct data The webapp itself is not deployed yet (could this be it?)

Any ideas/help would be appreciated thx

Share Improve this question edited Dec 17, 2020 at 22:37 benomatis 5,6337 gold badges39 silver badges60 bronze badges asked Aug 16, 2018 at 18:20 Bob ReyniersBob Reyniers 1031 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

If you want your local emluation of functions using firebase serve to pick up environment variables, you need to follow this bit of instruction from the 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

Why not using .env file from the functions directory? It catches the environmental variables automatically.

发布评论

评论列表(0)

  1. 暂无评论