I am writing a firebase functions as below . Problem is it is not getting triggered when I am adding a new user. Please advice me where I am doing mistake.
My exception with below function is that whenever an entry goes to /user
document, an email should go to the user email. Basically it is a signUp page, for a successful sign-up I would like to send email.
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as sendgrid from '@sendgrid/mail';
admin.initializeApp(functions.config().firebase);
// const SENDGRID_API_KEY = functions.config().sendgrid.key;
const SENDGRID_API_KEY = 'SG.xxxxCfXA46A.6i-lhxxxxxxxxxxx0';
// const sendgrid = require('@sendgrid/mail');
sendgrid.setApiKey(SENDGRID_API_KEY);
exports.userCreateEmail = functions.database.ref('/users/{userId}').onCreate((event) => {
const userId = event.params.userId;
console.info('userId '+userId);
const db = admin.firestore();
return db.collection('users').doc(userId).get().then(doc => {
const user = doc.data();
console.log(user);
const msg = {
to: user.email,
from: 'asdf@gmail.com',
subject: 'new follower',
templateId: 'b98f5d37-8371-4c24-850c-sdfasdf'
};
return sendgrid.send(msg);
}).then(() => console.log('email sent!'))
.catch(err => console.log(err))
});