简体   繁体   中英

Firebase functions integrating with Sendgrid

I'm fairly new with Firebase functions and I'm trying to create a simple onCreate() trigger however I cant seem to get it up and running.

Am I not returning the promise correctly with Sendgrid? Not sure what I am missing

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const sendGrid = require("@sendgrid/mail");
admin.initializeApp();

const database = admin.database();
const API_KEY = '';
const TEMPLATE_ID = '';
sendGrid.setApiKey(API_KEY);

const actionCodeSettings = {
  ...
};

exports.sendEmailVerify = functions.auth.user().onCreate((user) => {
  admin
    .auth()
    .generateEmailVerificationLink(user.email, actionCodeSettings)
    .then((url) => {
      const msg = {
        to: user.email,
        template_id: TEMPLATE_ID,
        dynamic_template_data: {
          subject: "test email",
          name: name,
          link: url,
        },
      };
      return sendGrid.send(msg);
    })
    .catch((error) => {
      console.log(error);
    });
});

Logs from firebase functions

sendEmailVerify
Function execution started

sendEmailVerify
Function returned undefined, expected Promise or value

sendEmailVerify
Function execution took 548 ms, finished with status: 'ok'

sendEmailVerify
{ Error: Forbidden

sendEmailVerify
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:133:29)

sendEmailVerify
at process._tickCallback (internal/process/next_tick.js:68:7)

sendEmailVerify
code: 403, 

sendEmailVerify
message: 'Forbidden', 

You are not correctly returning the Promises chain in your Cloud Function. You should do as follows:

exports.sendEmailVerify = functions.auth.user().onCreate((user) => {
  return admin // <- See return here
    .auth()
    .generateEmailVerificationLink(user.email, actionCodeSettings)
    .then((url) => {
      const msg = {
        to: user.email,
        template_id: TEMPLATE_ID,
        dynamic_template_data: {
          subject: "test email",
          name: name,
          link: url,
        },
      };
      return sendGrid.send(msg);
    })
    .catch((error) => {
      console.log(error);
      return null;
    });
});

There are at least two programming problems here.

  1. You're not returning a promise from the function that resolves when all the async work is complete. This is a requirement. Calling then and `catch is not sufficient. You actually have a return a promise from the function handler.

  2. You're calling sendGrid.send(email) , but you never defined a variable email anywhere in the code. If this is the case, then you're passing an undefined value to sendgrid.

There is also the possibility that your project is not on a payment plan, in which case, the call to sendgrid will always fail due to lack of outbound networking on the free plan. You will need to be on a payment plan for this to work at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM