Send transactional email with SendGrid in cloud functions Firebase

回眸只為那壹抹淺笑 提交于 2019-12-11 02:25:34

问题


I followed this tutorial: https://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/ to send transantional emails. The next function was working normally but with the new update of the google cloud functions https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore has stopped working. What should I change?

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = functions.config().sendgrid.key

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);

exports.firestoreEmail = functions.firestore
.document('mensajes/{mensajeId}')
.onCreate(event => {

    const mensajeId = event.params.mensajeId;

    const db = admin.firestore()

    return db.collection('mensajes').doc(mensajeId)
        .get()
        .then(doc => {

            const mensaje = doc.data()

            const msg = {
                to: 'xx@xx.com',
                from: 'zz@zz.com',
                subject: 'Subject',
                templateId: 'myTemplateID',
                substitutionWrappers: ['{{', '}}'],
                substitutions: {

                    nombre: mensaje.nombre,
                    telefono: mensaje.telefono,
                    email: mensaje.email,
                    mensaje: mensaje.mensaje

                }
            };

            return sgMail.send(msg)
        })
        .then(() => console.log('email sent!'))
        .catch(err => console.log(err))


});

回答1:


Following the release of Cloud Functions 1.0.x , the following has changed for Firestore, as you can deduce form the doc:

Before (<= v0.9.1)

exports.dbCreate = functions.firestore.document('notes/{noteId}').onCreate((event) => {
    const newData = event.data.data();
    const param = event.params.noteId;
});

Now (v1.0.0)

exports.dbCreate = functions.firestore.document('notes/{noteId}').onCreate((snap, context) => {
  const newData = snap.data(); 
  const param = context.params.noteId;
});

So, in your case, that means your Cloud Function shall be changed to:

exports.firestoreEmail = functions.firestore
.document('mensajes/{mensajeId}')
.onCreate(event => {

    const mensajeId = context.params.mensajeId;  // <- Here is the change

    const db = admin.firestore()

    return db.collection('mensajes').doc(mensajeId)
        .get()
        .then(doc => {...
        .....



回答2:


Cloud Functions Documentation

SendGrid Tutorial

This tutorial demonstrates using Cloud Functions to send emails through the SendGrid platform, receive SendGrid analytics data via webhooks, and load the analytics data into Google BigQuery for analysis.

Objectives

  • Create a SendGrid account.
  • Write and deploy two HTTP Cloud Functions.
  • Write and deploy one Background Cloud Function.
  • Send an email from the deployed function via SendGrid.
  • Receive analytics data from SendGrid via webhooks.
  • Load SendGrid analytics data into BigQuery for analysis.



来源:https://stackoverflow.com/questions/50205390/send-transactional-email-with-sendgrid-in-cloud-functions-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!