电子邮件数据库结果

Email Database Results

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

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

const val = snapshot.val();

请注意,handling event data for Realtime Database triggered Cloud Functions.

文档中的示例显示了这一点

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