简体   繁体   中英

Firebase Functions ReferenceError with SendGrid function

I'm trying to get started with SendGrid and a Firestore database, using Firebase functions. I've gone through tutorials and set up according to the latest notation, (snap, context) instead of (event). I can't figure out what's wrong with this script:

// 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) )
    });

The error message generated in the Firebase console is mysterious, and I'm not even sure how to interpret it.

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

)

My best guess just that my 'confEmail' function is not defined because there's an error in it, but I can't figure out what. Or does it mean something else?

It looks like most of the tutorial scripts are over-complicating things. and a simpler script like this seems to work.

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)

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM