-1

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))
});
Chandrika
  • 194
  • 12
Vijay Keshri
  • 427
  • 1
  • 4
  • 12
  • Please edit your question to explain exactly what it's supposed to do, and the change you made that expects it to be invoked. In particular, you're apparently waiting for a change in Realtime Database, but then doing work in Firestore after that, which doesn't make sense. – Doug Stevenson Mar 09 '18 at 07:06

1 Answers1

0

If using firebase:

Change this:

exports.userCreateEmail = functions.database.ref(`/users`).onCreate((event) => {

to this:

exports.userCreateEmail = functions.database.ref('/users/{userId}').onCreate((event) => {

Also use ' instead of `

Also event.params will retrieve the value of the wildcard, so you have to add {//anyname} as wild card and then you will be able to retrieve it.

Also retrieve the data using val() instead of data()

If you are using firestore then you need to use this:

exports.userCreateEmail = functions.firestore.document('users/{userId}').onCreate((event) => {
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I made changes as per your comment , still having same issue. – Vijay Keshri Mar 09 '18 at 08:09
  • make sure that the document name is correct, "user" should be the same as the document name in the console @VijayKeshri – Peter Haddad Mar 09 '18 at 08:10
  • Yes, I am using firestore cloud, If I am making changes exports.userCreateEmail = functions.firestore.document('/users/{userId}').onCreate( (event) => { – Vijay Keshri Mar 09 '18 at 08:16
  • is this `console.info('userId '+userId);` appearing in the console? – Peter Haddad Mar 09 '18 at 08:20
  • 1:49:35.796 PM info userCreateEmail { Error: getaddrinfo ENOTFOUND api.sendgrid.com api.sendgrid.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'api.sendgrid.com', host: 'api.sendgrid.com', port: 443 } – Vijay Keshri Mar 09 '18 at 08:22
  • Yes, console.log is printing userId. I am getting an error on log for sending email. – Vijay Keshri Mar 09 '18 at 08:24
  • 2018-03-09T08:37:32.698Z I userCreateEmail: { uid: 'yzgTbeesSEdgHEFICLFNBljgDPA2', photoURL: 'https://lh3.gxxx/photo.jpg', email: 'xx@gmail.com', displayName: 'xx' } 2018-03-09T08:37:32.932Z I userCreateEmail: { Error: getaddrinfo ENOTFOUND api.sendgrid.com api.sendgrid.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'api.sendgrid.com', host: 'api.sendgrid.com', port: 443 } – Vijay Keshri Mar 09 '18 at 08:40
  • @VijayKeshri okay I see, check this: https://stackoverflow.com/questions/17690803/node-js-getaddrinfo-enotfound – Peter Haddad Mar 09 '18 at 08:59