0
votes

I am sending an email to a person whose data is updated which is stored in google firestore, using cloud functions. I am using sendgrid to send email.

My cloud function is working fine, whenever I update the data it gets triggered. But I am unable to send email.

const sendgridemail = require('@sendgrid/mail');
const MY_SENDGRID_API_KEY = '<API key>'
sendgridemail.setApiKey(MY_SENDGRID_API_KEY);
exports.helloFirestore = (event, callback) => {
const triggerResource = event.resource;
console.log('Function triggered by change to: ' +  triggerResource);
console.log(JSON.stringify(event));

 const msgbody = {
                    to: '[email protected]',
                    from: '[email protected]',
                    subject:  'database updated - xyzshopping.com',
                    templateId: '<template ID>',
 }
return helloFirestore.send(msgbody)

            .then(() => console.log('payment mail sent success') )
            .catch(err => console.log(err) )
             callback();


};

I have deployed the code from console using inline editor and zip upload also. It is getting triggered but not sending email.It is throwing me error:

Error: getaddrinfo ENOTFOUND api.sendgrid.com api.sendgrid.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'api.sendgrid.com', host: 'api.sendgrid.com', port: 443

1

1 Answers

1
votes

You're supposed to call callback() only after all the async work is done. You're calling it before the work even starts. That's certainly not going to work. From the documentation:

callback

A callback to signal completion of the function's execution.

You've signaled the end of the function prematurely. You should only call it after the work completes, both in the then and catch callbacks of the async work.