电子邮件数据库结果

时间:2019-05-31 19:50:49

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

我有一个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;
});

2 个答案:

答案 0 :(得分:0)

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

const val = snapshot.val();

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

答案 1 :(得分:-1)

弄清楚了。我必须创建一个新变量const newvar = JSON.stringify(val),然后说文本:newvar

相关问题