javascript - 通过 Firebase Cloud 函数提取特定节点值

标签 javascript firebase firebase-realtime-database google-cloud-functions

好的,我将从一些背景开始(跳到“我的问题”):

我正在开发一个应用程序,它将数据以二维数组的形式从 Google Sheet 传递到 Firebase 实时数据库。 Google Sheet的数据布局如下图: Data in the Google Sheet

此数据通过 Apps 脚本函数传递到节点 masterSheet 下的 Firebase 实时数据库,结果如下所示:

Data in the Firebase Realtime Database

它用作我正在使用 Ionic 框架开发的移动 Web 应用程序的实时数据库(预览如下):

Data as viewed on the Ionic App

我有处理在每个作业的子任务的正确位置设置“Y”和“N”标志的函数,以及当所有子任务时将整体作业完成状态标志设置为“Y”的函数已按预期完成工作。

我正在尝试通过 Firebase Cloud Functions 添加自动电子邮件服务,只要作业的总体“已完成”状态设置为“Y”(即 ref: 'masterSheet/中的值),该服务就会发送“作业完成通知” 0/1' 等于“Y”)。

到目前为止,每当作业的整体完成状态从“N”更改时,我已设法使用 nodemailer 和 Firebase Admin SDK 通过 Firebase 云功能成功向 Firebase 应用的所有注册用户发送电子邮件通过 onUpdate() 方法和要监听的位置的 .ref() 到“Y”。

下面是我的 Index.js 文件,其中包含我正在使用的云函数:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

// The mail service used
const nodemailer = require('nodemailer');

// Cloud Fucntion to export:
exports.onMessageUpdate = functions.database.ref('/masterSheet/{subArray}/1')
  .onUpdate((change) => {
    var changeRef = change.after.ref.path;
    console.log('changeRef: ' + changeRef);
    var newVal = change.after.val();
    if (newVal == "Y"){
      getUsers();
    }
  })

// Fucntion to get all registers users of the Firebase Project
function getUsers(){
  var userEmails = [];
  admin.auth().listUsers()
    .then(function(listUsersResult) {
      listUsersResult.users.forEach(function(userRecord) {
        console.log(userRecord);
        userEmails.push(userRecord.email);
        sendCompletionEmail(userRecord.email)
      });
    })
    .catch(function(error) {
      console.log("Error listing users:", error);
    });
}

// Function to send automatic emails
function sendCompletionEmail(email){
  var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      type: 'OAuth2',
      user: 'xxxxxxxx@gmail.com',
      clientId: 'xxxxxxxx.apps.googleusercontent.com',
      clientSecret: 'xxxxxxxxxxxxxxx',
      refreshToken: 'xxxxxxxxxxxxxx'
    }
  })

  // Email details:
  var mailOptions = {
    from: 'xxxxxxx',
    to: email,
    subject: 'Job completion notification',
    text: 'This is an automated message to inform you that a job has been fully completed ' + 
    'with all of its tasks marked as done. \n\nYou can view this (along with any others) from the Completed ' +
    'Jobs page within the app.'
  }

  transporter.sendMail(mailOptions, function (err, res) {
    if(err){
        console.log('Error');
    } else {
        console.log('Email Sent');
    }
  })
}

我的问题: 我希望能够在自动发送的电子邮件中包含职位名称。

记录下面代码片段中使用的change.after.ref.path的结果:

// Cloud Fucntion to export:
exports.onMessageUpdate = functions.database.ref('/masterSheet/{subArray}/1')
  .onUpdate((change) => {
    var changeRef = change.after.ref.path;
    console.log('changeRef: ' + changeRef);
    var newVal = change.after.val();
    if (newVal == "Y"){
      getUsers();
    }
  })

产生此日志输出:

changeRef console log

其中包含了我想要的内容...但我不知道如何将其取出...

如何从 changeRef 变量中检索第二个值,以便将其传递到 sendCompletionEmail() 函数并使用它来引用位置处的项目[0] 对于该节点?

类似于:

var subArray = changeRef[1]

从 masterSheet/0/1 中获取值:0

我可以将其存储为变量并用于引用发送的电子邮件中刚刚完成的工作的职位名称。

感谢您的帮助!

最佳答案

如果您要从请求中查找 0,则可以从传递到您的 Cloud Function 的第二个参数中获取该值(但您未声明)。

exports.onMessageUpdate = functions.database.ref('/masterSheet/{subArray}/1')
  .onUpdate((change, context) => {
      console.log(context.params.subArray);
  })

请参阅 handling event data 上的 Firebase 文档以及 onUpdate 的引用文档.

关于javascript - 通过 Firebase Cloud 函数提取特定节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54790560/

相关文章:

javascript - 网站上每个页面的不同计数器值

javascript - 如何在 json 数组中搜索值,如果找到值则删除索引

javascript - 跨页面传递 json 值

javascript - 如何根据图库中图像名称的前缀过滤图像

mysql - Firebase 和 Mysql 一起使用

javascript - Firebase 数据库触发器 - 如何等待调用

javascript - firebase数据库中如何同时将数据存储在两个地方?

firebase - 如何从 Firebase 云功能访问 bigquery 数据?

firebase - 如何调试 Firebase 安全规则/权限

java - addChildEventListener (Firebase) 中的数组问题