javascript - Firebase 云功能在发送电子邮件之前结束

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

显然下面的代码工作正常,但是触发器函数的执行在电子邮件发送之前很久就结束了。 不知道会不会导致错误。

延迟结束直至电子邮件发送的正确方法是什么?

const mailTransport = nodemailer.createTransport({
    service: "gmail",
    auth: {
        user: "email",
        pass: "pass"
      }
});

exports.createEnrollment = functions.firestore
    .document("enrollments/{enrollmentId}")
    .onCreate(event => {

        var newValue = event.data.data();

        // ... other commands to retrieve email content and recipient

         mailTransport
          .sendMail(content)
          .then(() => {
            console.log(`New email sent to ${email}`);
          })
          .catch(function(error) {
            console.error("Error writing document: ", error);
          });

     console.log("ending function");
     return event.data.data();

});

最佳答案

在你的 promise 兑现之前你就要回来了。

const mailTransport = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "email",
    pass: "pass"
  }
});

exports.createEnrollment = functions.firestore
  .document("enrollments/{enrollmentId}")
  .onCreate(event => {

    var newValue = event.data.data();    

    // ... other commands to retrieve email content and recipient

    return mailTransport
      .sendMail(content)
      .then(() => {
        console.log(`New email sent to ${email}`);
      })
      .catch(function (error) {
        console.error("Error writing document: ", error);
      });
    console.log("ending function");
  });

mailtransport.sendMail 是一个应该返回的 promise 。

但是现在你将永远不会在日志中看到结束函数..如果你愿意的话。将其移至您的 .then 函数中,或链接另一个函数。

关于javascript - Firebase 云功能在发送电子邮件之前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48990488/

相关文章:

javascript - atom ide 是如何工作的

reactjs - 带有 React.createContext 的 Typescript HOC

javascript - 在云函数中获取文档值

firebase - Stripe firebase扩展,监听收集组中的发票收集

javascript - 无法读取 admin.firestore.collection.doc.get.then.doc 处未定义的属性 'firstName'

javascript - 父子之间使用 row ReactJs 进行通信

JavaScript jQuery 依赖下拉列表

javascript - 使用 Jquery 无法在 Div 中加载 html 页面

ios - 让条件在 Firebase Observer 中工作

javascript - “没有创建 Firebase 应用程序 '[DEFAULT]'”,即使调用了 initializeApp