Firestore云功能 - 使用SendGrid发送电子邮件onCreate

问题描述 投票:0回答:1

我有一个联系表单,用于向Firestore数据库提交数据。我的意图是,只要在requests集合中有另一个条目,Firestore就会通过Cloud Function触发一个函数,它包含SendGrid的配置,SendGrid也应该将此特定条目的数据发送到电子邮件。

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

无法读取未定义的属性'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: '[email protected]',
          from: '[email protected]',

          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: '[email protected]',
          from: '[email protected]',

          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个回答
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: '[email protected]',
             from: '[email protected]',

             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) )
  });
© www.soinside.com 2019 - 2024. All rights reserved.