Node.js 电子邮件数据库结果

Node.js 电子邮件数据库结果,node.js,firebase-realtime-database,google-cloud-functions,nodemailer,Node.js,Firebase Realtime Database,Google Cloud Functions,Nodemailer,我有一个firebase数据库,onCreate Google Cloud函数调用NodeEmailer并向我发送电子邮件。这一切都很好,但现在我还试图在电子邮件中包含添加到数据库中的数据。我无法让它工作,似乎是因为它不是文本格式,我尝试过转换成文本,但似乎没有。我做错了什么 const functions = require('firebase-functions'); const nodemailer = require('nodemailer'); // Configure the ema

我有一个firebase数据库,onCreate Google Cloud函数调用NodeEmailer并向我发送电子邮件。这一切都很好,但现在我还试图在电子邮件中包含添加到数据库中的数据。我无法让它工作,似乎是因为它不是文本格式,我尝试过转换成文本,但似乎没有。我做错了什么

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;
});
const functions=require('firebase-functions');
const nodemailer=require('nodemailer');
//使用默认SMTP传输和GMail帐户配置电子邮件传输。
//有关其他类型的传输,如Sendgrid,请参阅https://nodemailer.com/transports/
//TODO:配置`gmail.email`和`gmail.password`谷歌云环境变量。
const gmailEmail=functions.config().gmail.email;
const gmailPassword=functions.config().gmail.password;
const mailTransport=nodemailer.createTransport({
服务:“gmail”,
认证:{
用户:Gmail,
密码:gmailPassword,
},
});
exports.sendWelcomeEmail=functions.database.ref('/PickupRequests/{pushId}'))
.onCreate(异步(快照、上下文)=>{
const val=snapshot.data;
常量邮件选项={
从:“”,
至:mike@puravidalaundry.com",
主题:“新收货请求”,
text:val//如何将其转换为文本格式?
};
试一试{
等待mailTransport.sendMail(mailpoptions);
console.log(“已发送电子邮件”);
}捕获(错误){
console.error('发送电子邮件时出错:',错误);
}
返回null;
});

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

const val = snapshot.val();

请注意,上的文档中的一个示例显示了这一点。

解决了这一问题。我必须创建一个新变量const newvar=JSON.stringify(val),然后我说text:newvar

好的,我再进一步,谢谢。然而,现在电子邮件上只说“[对象]”就明白了。我必须创建一个新变量const newvar=JSON.stringify(val),然后我说text:newvar