2
votes

I want to send an email when a new order is created in my firebase database, but nothing happens when I create an order. My function:

    exports.sendEmailConfirmation = functions.database.ref('/orders').onCreate(event => { 

  const mailOptions = {
    from: '"Someone." <[email protected]>',
    to: '[email protected]',
  };

  // Building Email message.
  mailOptions.subject = 'New order from mobile app!';
  mailOptions.text = 'John Doe lorem ipsum';

  return mailTransport.sendMail(mailOptions)
    .then(() => console.log('¡¡¡ Enail sent !!!'))
    .catch((error) => console.error('Error!!', error));
});

This code works using onWrite()....

1

1 Answers

6
votes

Your function isn't triggering because /orders already exists. onCreate triggers will only run when the path you specify is newly created.

If you want to know when a child is newly added under /orders, you should use a wildcard in the path:

functions.database.ref('/orders/{orderId}')