【问题标题】:Firestore Cloud Function - Send E-Mail onCreate with SendGridFirestore 云功能 - 使用 SendGrid 发送电子邮件 onCreate
【发布时间】:2019-08-08 20:43:05
【问题描述】:

我有一个向 Firestore 数据库提交数据的联系表单。我的意图是,一旦集合requests 中有另一个条目,Firestore 将通过 Cloud Function 触发一个函数,该函数包含 SendGrid 的配置,该配置再次应该将此特定条目的数据发送到 e-邮件。

我也尝试过部署此功能,它是成功的 - 但控制台显示以下错误,我认为不会是唯一的错误:

无法读取未定义的属性“requestId”

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

const admin = require('firebase-admin');
admin.initializeApp();

const SENDGRID_API_KEY = functions.config().sendgrid.key;

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

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

    const requestId = event.params.requestId;

    const db = admin.firestore();

    return db.collection('requests').doc(requestId)
      .get()
      .then(doc => {

        const requestId = event.params.requestId;

        const request = doc.data();

        const msg = {
          to: 'fuh@gmx.net',
          from: 'hello@angularfirebase.com',

          templateId: 'd-',
          substitutionWrappers: ['{{', '}}'],
          substitutions: {
            name: request.name,
            lastname: request.lastname,
            email: request.email,
            package: request.package,
            date: request.date,
            text: request.text
            // and other custom properties here
          }
        };

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


  });

编辑:

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

const admin = require('firebase-admin');
admin.initializeApp();

const SENDGRID_API_KEY = functions.config().sendgrid.key;

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

exports.request = functions.firestore
  .document('requests/{requestId}')
  .onCreate((snap, context) => {

    const db = admin.firestore();

    return db.collection('requests').doc(requestId)
      .get()
      .then(doc => {

        const requestId = snap.id;

        const request = doc.data();

        const msg = {
          to: 'fuhr@gmx.net',
          from: 'hello@angularfirebase.com',

          templateId: 'd-3cd6b40ad6674f33702107d2',
          substitutionWrappers: ['{{', '}}'],
          substitutions: {
            name: request.name,
            lastname: request.lastname,
            email: request.email,
            package: request.package,
            date: request.date,
            text: request.text
            // and other custom properties here
          }
        };

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


  });

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions sendgrid


    【解决方案1】:

    .onCreate() 方法不返回 event,它返回对象的快照,并从中获取新对象的 id。

    所以在你的情况下,它必须是:

    exports.firestoreEmail = functions.firestore.document('requests/{requestId}')
      .onCreate((snap, context) => {
    
        const requestId = snap.id; // get the id
        const db = admin.firestore();
    
        return db.collection('requests').doc(requestId)
            .get()
            .then(doc => {
               const request = doc.data();
               const msg = {
                 to: 'fuhr@gmx.net',
                 from: 'hello@angularfirebase.com',
    
                 templateId: 'd-3cd6b40ad6674f33702107d2',
                 substitutionWrappers: ['{{', '}}'],
                 substitutions: {
                     name: request.name,
                     lastname: request.lastname,
                     email: request.email,
                     package: request.package,
                     date: request.date,
                     text: request.text
                     // and other custom properties here
                 }
             };
    
             return sgMail.send(msg)
         })
         .then(() => console.log('email sent!') )
         .catch(err => console.log(err) )
      });
    

    【讨论】:

    • 请注意functions.firestore .document('requests/{requestId}') .onCreate(event => {});是旧语法,对于版本firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore
    • 感谢您的帮助。我已经部署了新代码,Firebase 上的错误消失了,但电子邮件仍然不会发送到“to:''”字符串。
    • 您是要发送电子邮件至fuh@gmx.net 还是从firestore 获得的某个动态电子邮件地址?
    • @HristoEftimov 我正在尝试发送到 fuh@gmx.net
    • @HristoEftimov 刚刚出现以下错误:ReferenceError: the event is not defined
    猜你喜欢
    • 2021-06-18
    • 2019-05-21
    • 2018-10-16
    • 2018-08-17
    • 2017-12-03
    • 2020-11-22
    • 2018-11-08
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多