【问题标题】:Email Database Results电子邮件数据库结果
【发布时间】:2019-10-17 10:50:57
【问题描述】:

我有一个 firebase 数据库,其中 onCreate Google Cloud Functions 调用 nodemailer 并向我发送电子邮件。一切正常,但现在我正在尝试在电子邮件中包含添加到数据库中的数据。我无法让它工作,似乎是因为它不是文本格式,我已经尝试转换为文本,但似乎没有这样做。我做错了什么?

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
 // TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword,
  },
});

exports.sendWelcomeEmail =      functions.database.ref('/PickupRequests/{pushId}')
.onCreate(async(snapshot, context) => {

  const val = snapshot.data;

  const mailOptions = {
    from: '<noreply@firebase.com>',
    to: "mike@puravidalaundry.com",
    subject: "New Pickup Request",
    text: val //How do i convert this to a text format?
  };

  try {
    await mailTransport.sendMail(mailOptions);
    console.log('email sent');
  } catch(error) {
    console.error('There was an error while sending the email:',     error);
  }
  return null;
});

【问题讨论】:

    标签: node.js firebase-realtime-database google-cloud-functions nodemailer


    【解决方案1】:

    要从快照中获取值,请使用snapshot.val()。所以:

    const val = snapshot.val();
    

    请注意,这在 handling event data for Realtime Database triggered Cloud Functions 上的文档中的示例中显示。

    【讨论】:

    • 好的,我说得更远了,谢谢。但是,现在电子邮件只显示“[object Object]”
    • 想通了。我必须创建一个新变量 const newvar = JSON.stringify(val) 然后我说 text: newvar
    【解决方案2】:

    想通了。我必须创建一个新变量 const newvar = JSON.stringify(val) 然后我说 text: newvar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2010-12-28
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多