【问题标题】:NodeJs TypeError: Cannot read property 'key' of undefinedNodeJs TypeError:无法读取未定义的属性“键”
【发布时间】:2018-08-17 20:39:29
【问题描述】:

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

这是我的功能:

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" <noreply@clate.de>',
            to: 'locations@clate.de',
            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))
    }

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 你能详细解释一下吗?
  • 看起来代码来自教程,我想您跳过了设置环境变量的步骤。追溯步骤并确保您没有遗漏任何内容...
猜你喜欢
  • 2017-03-06
  • 2022-09-23
  • 2022-11-29
  • 2017-08-30
  • 2017-10-11
  • 1970-01-01
  • 2023-03-19
  • 2018-06-02
  • 2020-03-25
相关资源
最近更新 更多