1
votes

I have created a Firebase cloud function that will trigger on update of the data. When I go into Firebase console and change the node to either true or false, it triggers and I receive an email from my SendGrid set up. The problem is I am not able to obtain the users e-mail information.

I have spent over a week pouring over the documentation and it says I should be able to use context.auth, however, that is always "undefined" when printed out in console.

I have been trying to get the user data from the users actual info in Firebase as well as in /users/{uid}/email. I can't seem to figure out how to get the e-mail since the snapshot is in a different spot.

I need to somehow extract the users first name and email, which are in in: /users/uid/first_name and /users/uid/email

I want those two things put into this function, so then I can tell SendGrid to use the email and name. The Sendgrid portion is working fine.

context.params.uid gives me the users firebase ID, but does nothing for me. I can't seem to use that to get the data I need

I tried authVar = context.auth and when I print it out it says 'undefined' and my function stops working.

exports.myFunctionPending = 
functions.database.ref('/users/{uid}/profile/isPending')
.onUpdate(async (change, context) => {
    const snapshot = change.after;
    const val = snapshot.val();
    const userid = context.params.uid;  //shows userid but is useless
    const authVar = context.auth;  //says undefined and does nothing

    console.log(val);
    console.log(userid);

               const msg = {
                    to: '[email protected]',
                    from: '[email protected]',

                    // custom templates
                    templateId: 'd-b7aakjsdgwq7d798wq7d8',
                    substitutionWrappers: ['{{', '}}'],

                    //substitutions: {
                    dynamic_template_data: {
                    //name: user.displayName
                      name: 'My Name'

                    }
                };
 try {
   await sgMail.send(msg);
   console.log('This was sucessful');
 } catch(error) {
   console.error('There was an error while sending the email:', error);
 }
 return null;
});
1
If the data you're looking for is stored in Realtime Database, then you'll have to query it. This is very common. firebase.google.com/docs/database/admin/start - Doug Stevenson
@DougStevenson I had done that previously and I kept receiving an undefined for the value. I read online on several sites not to nest a query and use context.auth. I will follow the steps in the article again and see what happens. Thanks for your help. - Greg
@DougStevenson I followed the example exactly, and it is coming back as null, it won't read the snapshot. - Greg

1 Answers

0
votes

I had the code in the incorrect spot, I changed the logic and now it's working as intended.

exports.myFunction = functions.database.ref('/users/{uid}/user_area/pending')
    .onUpdate(async (change, context) => {
        const triggersnapshot = change.after;
        const val = triggersnapshot.val();
        const userid = context.params.uid;

        console.log(val);
        console.log(userid);

        return admin.database().ref('users/' + userid).once('value').then(function (snapshot) {
            var email = snapshot.child('email');
            var name = snapshot.child('first_name');
            console.log(snapshot.val());
            console.log(email.val());

            const msg = {
                to: [email],
                from: {
                    email: '[email protected]',
                    name: 'No Name'
                },
                // custom templates
                templateId: 'd-8347983274983u483242',
                substitutionWrappers: ['{{', '}}'],
                dynamic_template_data: {
                    name: name
                }
            };
            return sgMail.send(msg);

        });