Firebase获取特定密钥

赫克托·莫兰(Hector Moran)

我有realtime-database以下结构:

-- test-b7a6b
    -- locations
        -- 0
            -- logs
                -- alarm_2a330b56-c1b8-4720-902b-df89b82ae13a                
                ...
            -- devices
            -- deviceTokens
        -- 1
        -- 2

我正在使用的firebase-functions是在写入新日志时执行

let functions = require('firebase-functions');
let admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendPush = functions.database.ref('/locations/0/logs/{devicelogs}/{logs}').onWrite((change, context) => {
  let logsData = change.after.val();
  loadUsers();
  getBody(deviceTypeId);
  //managing the results
});

我还有其他功能要与新功能指向相同的位置 log

function loadUsers() {
    let dbRef = admin.database().ref('/locations/0/deviceTokens');
    //managing users
}

function getBody(deviceTypeId) {
  let devicesDB = admin.database().ref('/locations/0/devices');
  //managing devices
}  

将位置手动放置在所有3个功能上使其正常工作,但我不知道如何使其在所有位置(0、1和2上监听同一事件,并且将来可能还会在更多位置上监听

所以我的问题是:有什么办法可以在将日志写入任何位置时获取位置键,以便将其发送给其他功能

弗兰克·范普菲伦

要收听所有位置,请在触发函数的路径中使用参数:

exports.sendPush = functions.database.ref('/locations/{location}/logs/{devicelogs}/{logs}').onWrite((change, context) => {

然后,您可以从中获取参数context.params并将其传递:

exports.sendPush = functions.database.ref('/locations/{location}/logs/{devicelogs}/{logs}').onWrite((change, context) => {
  let logsData = change.after.val();
  loadUsers(context.params.location);
  getBody(deviceTypeId);
  //managing the results
});

另请参阅有关处理事件数据Cloud Functions for Firebase文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章