Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase functions environment variables can not read property of undefined

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

like image 951
Bob Reyniers Avatar asked Aug 16 '18 18:08

Bob Reyniers


1 Answers

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
like image 114
Doug Stevenson Avatar answered Nov 07 '22 03:11

Doug Stevenson