I am currently attempting to use the SendGrid API from a Firebase function to send a confirmation email.
The API itself is functioning properly, but I am encountering an issue where I am unable to retrieve the value of the child oncreate (as shown in the Firebase function log):
TypeError: Cannot read property 'participant_id' of undefined
at exports.SendEmail.functions.database.ref.onCreate.event (/user_code/index.js:15:38)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
at /var/tmp/worker/worker.js:716:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Here is the code snippet causing the issue:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.SendEmail = functions.database.ref('participants/{participant_id}').onCreate(event => {
const participant_id = event.params.participant_id;
const db = admin.database();
return db.ref(`participants/${participant_id}`).once('value').then(function(data){
const user = data.val();
const email = {
to: user.email,
from: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd8f989e8f88899890989389d39e948e8e8e9098cccbbd8e8e8e8ed39a92888bd38c9ed39e9c">[email protected]</a>',
subject: "Bienvenue au Blitz d'embauche 2018!",
templateId: 'dd3c9553-5e92-4054-8919-c47f15d3ecf6',
substitutionWrappers: ['<%', '%>'],
substitutions: {
name: user.name,
num: user.num
}
};
return sgMail.send(email)
})
.then(() => console.log('email sent to', user.email))
.catch(err => console.log(err))
});
I have worked with Firebase functions before and even used codes that were previously successful. However, I am still getting an undefined value!
Could the issue be related to any changes made by Firebase to event.params
?
Additionally, my participant_id is an integer value (e.g., 3827). Could this be impacting the functionality?
Thank you for your assistance!