带SendGrid函数的Firebase函数ReferenceError

时间:2018-10-31 12:35:11

标签: firebase sendgrid

我正在尝试使用Firebase函数开始使用SendGrid和Firestore数据库。我已经阅读了教程,并根据最新的符号(快照,上下文)而不是(事件)进行设置。我无法弄清楚此脚本出了什么问题:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp();
//admin.initializeApp(functions.config().firebase);

const SENDGRID_API_KEY = my-api-key-is-here;

const sendgridemail = require('@sendgrid/mail');
sendgridemail.setApiKey(SENDGRID_API_KEY);

exports.confEmail = functions.firestore
    .document('clients/{clientId}/projects/{projectId}/form-data/{docId}') //any write to this node will trigger email
    .onCreate((snap, context) => {
        const clientId = context.params.clientId;
        const projectId = context.params.projectId;
        const docId = context.params.docId;
        const fsdb = admin.firestore();
        return fsdb.collection('clients/'+clientId+'/projects/'+projectId+'/form-data').doc(docId)
                 .get()
                 .then(doc => {

                    const docData = doc.data()

                    const msgbody = {
                        to: docData.EMAIL,
                        from: 'xxxxx@gmail.com',
                        subject:  'Form Submission Confirmation',
                        templateId: 'd-07bf6a2b89084951a30ceddcd9c8915f',
                        substitutionWrappers: ['{{', '}}'],
                        substitutions: {
                          formdata: "Message Body\n<br>"+docData.CONF_MSG
                        }
                    };

                    return confEmail.send(msgbody)
                })
                .then(() => console.log('confimration mail sent success') )
                .catch(err => console.log(err) )
    });

在Firebase控制台中生成的错误消息是神秘的,我什至不知道如何解释它。

ReferenceError: confEmail is not defined
    at fsdb.collection.doc.get.then.doc (/user_code/index.js:48:13)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7

我最大的猜测就是我的“ confEmail”函数未定义,因为其中存在错误,但我不知道是什么。还是其他意思?

1 个答案:

答案 0 :(得分:0)

看起来大多数教程脚本都过于复杂了。像这样的更简单的脚本似乎行得通。

const sendgrid = require('@sendgrid/mail');
sendgrid.setApiKey(SENDGRID_API_KEY);

exports.confEmail = functions.firestore
    .document('clients/{clientId}/projects/{projectId}/form-data/{docId}') //any write to this node will trigger email
.onCreate((snap, context) => {

    const docData = snap.data();

    const msgbody = {
        to: docData.EMAIL,
        from: 'xxxxxxx@gmail.com',
        subject:  'Form Submission Confirmation',
        templateId: 'd-07bf6a2b89084951a30ceddcd9c8915f',
        substitutionWrappers: ['{{', '}}'],
        substitutions: {
          formdata: docData.CONF_MSG
        }
    };

    return sendgrid.send(msgbody)

});