在Cloud功能Firebase中使用SendGrid发送交易电子邮件 [英] Send transactional email with SendGrid in cloud functions Firebase
问题描述
我遵循了本教程: https://angularfirebase .com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/发送跨域电子邮件.下一个功能正常运行,但是使用了Google云功能的新更新
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))
});
推荐答案
在发布Cloud Functions 1.0.x之后,Firestore发生了以下变化,因为您可以推断出
Following the release of Cloud Functions 1.0.x , the following has changed for Firestore, as you can deduce form the doc:
之前(< = v0.9.1)
exports.dbCreate = functions.firestore.document('notes/{noteId}').onCreate((event) => {
const newData = event.data.data();
const param = event.params.noteId;
});
现在(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 => {...
.....
这篇关于在Cloud功能Firebase中使用SendGrid发送交易电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!