2

i want to create a cloud function which sends email if some data where added to my database. Unfortunately while trying to deploy my function i receive this error: TypeError: Cannot read property 'key' of undefined

Here is my function:

const functions = require('firebase-functions')
const nodemailer = require('nodemailer')
const postmarkTransport = require('nodemailer-postmark-transport')
const admin = require('firebase-admin')

// 2. Admin SDK can only be initialized once
try {admin.initializeApp(functions.config().firebase)} catch(e) {
    console.log('dbCompaniesOnUpdate initializeApp failure')
 }

// 3. Google Cloud environment variable used:

const postmarkKey = functions.config().postmark.key
const mailTransport = nodemailer.createTransport(postmarkTransport({
auth: {
         apiKey: postmarkKey
}
  }))


 // // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase- 
 functions
//

exports.sendingEmailForlocationsRequests = 
 functions.database.ref('/clubs/{clubId}/{pushId}')
.onWrite((event) => {
//I want to retrieve the pushID        


return sendEmail();
})

    function sendEmail() {
    // 5. Send welcome email to new users
    const mailOptions = {
            from: '"Dave" <[email protected]>',
            to: '[email protected]',
            subject: 'Welcome!',
            html: `<Test>`
    }
    // 6. Process the sending of this email via nodemailer
    return mailTransport.sendMail(mailOptions)
            .then(() => console.log('dbCompaniesOnUpdate:Welcome 
    confirmation email'))
            .catch((error) => console.error('There was an error while 
    sending the email:', error))
    }
2
  • In which part you get the error? Commented Aug 17, 2018 at 20:40
  • Here's a working script/example for Firestore Commented Aug 17, 2018 at 21:16

1 Answer 1

3

It looks like 'postmark' isn't set in your firebase configuration. You can set what's retrieved by functions.config() using the CLI: https://firebase.google.com/docs/functions/config-env

Sign up to request clarification or add additional context in comments.

2 Comments

could you explain it more in detail?
it looks like the code comes from a tutorial, I'd imagine you skipped a step that set the environment variables. Retrace the steps and make sure you didn't miss anything...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.