【问题标题】:Variables from firebase in nodemailer inside cloudfunctions来自cloudfunctions中nodemailer中firebase的变量
【发布时间】:2018-12-11 17:02:42
【问题描述】:

我在 firebase cloudfunctions 中使用 nodemailer 在多个触发器上向用户发送电子邮件。

基本上它可以工作,我无法理解的一件事是:我的超级管理员被允许编写新的邮件模板,这些模板以字符串的形式保存到 firebase 数据库中。例如,假设它包含一个像这样的字符串形式的标题:

"Welcome to my App ${status.userData.name}!" 

在我的云函数中,当触发发生时,我会得到这样的创建模板之一:

状态是来自触发器数据库查询的对象:

exports.sendApplication = functions.database.ref('/xx/{userId}/').onUpdate((change, context) => {
  let status = change.after.val();
....

let messageRef = admin.database().ref('users').child('admin').child('template').child('welcome');
messageRef.once('value', snap => {
  let msg = snap.val();
  console.log('message: ', msg);
  const mailOptions = {
    from: `${status.userData.email}`,
    to: `${status.userData.email}`,
    subject: `${msg.title}` 
    html: `
      <h1>${msg.title}</h1>
      ${msg.message}
    `....

msg.title 的console.log 给了我这个:

"Welcome to my App ${status.userData.name}!"

status.userData.name 的console.log 给了我

"prename surname"

问题是,在我收到的电子邮件中仍然是${status.userData.name}

感谢任何帮助!

【问题讨论】:

  • 什么是状态?
  • 如果 status 没有值,它不会被插入到字符串中。仅供参考,如果字符串中唯一的东西是变量,则不需要字符串插值。你可以直接说status.userData.email,而不是${status.userData.email}
  • status 是来自触发器的数据库对象,它不为空,例如 status.userData.email 将是一个字符串,其中包含用户的邮件地址 - 例如test@rest.com 在问题中也澄清了这一点
  • 我现在尝试了你的建议,仍然是同样的问题: ${msg.title} 或 ' + msg.title + '... 在发送的邮件中给出相同的输出:这是 msg 的字符串.title(包含我想在邮件中显示的变量)

标签: javascript firebase google-cloud-functions nodemailer


【解决方案1】:

如果有人偶然发现: 解决方案其实很简单:

由于变量 ${msg.title} 包含字符串中的变量,我只需要将字符串中的变量替换为@Doug Stevenson 建议的实际变量:

msg.title = msg.title.replace("${status.userData.email}", status.userData.email);

【讨论】:

    猜你喜欢
    • 2020-12-02
    • 2020-11-04
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多