Trying to handle a contact form submission with Cloud Functions to send the email. The 'Hello World' function fired ok, so I think the set up is fine. The form populates the 'messages' collection, but I'm not getting a log entry (or error) for the trigger on the following:
const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref();
//if user contacts us
exports.sendContactEmail = functions.database
.ref('messages/{msgId}')
.onCreate(event => {
console.log('Made it to the trigger!');
const formData = event.data.data(); // the contact form data
return sendContactEmail(formData);
})
// Sends an email to someone at the company
function sendContactEmail(formInfo) {
console.log('Made it to the send function!');
...
package.json looks like:
"dependencies": {
"firebase-admin": "~5.8.1",
"firebase-functions": "^0.8.1",
The posted code is for a Realtime Database trigger. To trigger the function on creation of a Firestore document, change the code to this:
//if user contacts us
exports.sendContactEmail = functions.firestore // <= CHANGED
.document('messages/{msgId}') // <= CHANGED
.onCreate(event => {
console.log('Made it to the trigger!');
const formData = event.data.data(); // the contact form data
return sendContactEmail(formData);
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With