node.js - NodeJs 类型错误 : Cannot read property 'key' of undefined

标签 node.js firebase firebase-realtime-database google-cloud-functions

我想创建一个云函数,如果某些数据添加到我的数据库中,它会发送电子邮件。不幸的是,在尝试部署我的函数时,我收到此错误: 类型错误:无法读取未定义的属性“key”

这是我的功能:

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" <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1514091e0b17023b18171a0f1e551f1e" rel="noreferrer noopener nofollow">[email protected]</a>>',
            to: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e02010d0f1a0701001d2e0d020f1a0b400a0b" rel="noreferrer noopener nofollow">[email protected]</a>',
            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))
    }

最佳答案

您的 Firebase 配置中似乎未设置“邮戳”。您可以使用 CLI 设置functions.config() 检索的内容:https://firebase.google.com/docs/functions/config-env

关于node.js - NodeJs 类型错误 : Cannot read property 'key' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902571/

相关文章:

node.js - 无法在nodejs上运行coffeescript应用程序。接收异常

java - 删除firebase入口节点Android Java

angular - 如何使用 AngularFire、Firestore 和 Firebase 分页到上一页

android - Firebase 与 Realm。反序列化 POJO 类

android - 使用android数据绑定(bind)将数据从Recycler View onClick传递到Detail Activity

android - 如何使用列表/数组列表向 Firebase 添加和检索数据?

javascript - 如何将 pug 中的 every 与数组一起使用

javascript - sequelize 验证多列 [this.column got undefined]

mysql - 无法使用node-mysql分页

java - 如何让两种不同类型的用户自动登录不同的 Activity 而无需再次登录?