Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Function not triggering onCreate

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",
like image 753
pr0t0 Avatar asked Feb 27 '18 01:02

pr0t0


1 Answers

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);
})
like image 58
Bob Snyder Avatar answered Nov 06 '22 05:11

Bob Snyder