如何在Cloud Functions for Firebase中获取与事件无关的数据库值?

问题描述 投票:4回答:3

我有一个firebase数据库,我正在尝试使用云函数在我的数据库中的值更改时执行操作。到目前为止,它成功触发代码在我的数据库中的值更改时运行。但是,当数据库值更改时,我现在需要检查另一个值以确定其状态,然后执行操作。问题是我对JS有一些经验,除了部署,更改数据库中的值以及查看控制台日志之外,我无法调试我的代码。

有没有办法在数据库中查找另一个值并读取它?如何查找值然后为其设置值?这是代码:

exports.determineCompletion =

functions.database.ref('/Jobs/{pushId}/client_job_complete')
    .onWrite(event => {

        const status = event.data.val();
        const other = functions.database.ref('/Jobs/' + event.params.pushId + '/other_job_complete');
        console.log('Status', status, other);

        if(status == true && **other.getValueSomehow** == true) {
            return **setAnotherValue**;
        }


    });

此代码部分有效,它成功获取与client_job_complete相关联的值并将其存储在状态中。但是我如何获得其他价值呢?

此外,如果任何人有任何他们认为可以帮助我的JS或firebase文档,请分享!我在这里阅读了一堆firebase:https://firebase.google.com/docs/functions/database-events但它只讨论事件并且非常简短

谢谢您的帮助!

javascript firebase firebase-realtime-database google-cloud-functions
3个回答
6
投票

在编写数据库触发器函数时,该事件包含两个属性,这两个属性是对已更改数据位置的引用:

event.data.ref
event.data.adminRef

ref仅限于触发该功能的用户的权限。 adminRef可以完全访问数据库。

每个Reference对象都有一个root属性,它为您提供对数据库根目录的引用。您可以使用该引用在数据库的另一部分中构建引用的路径,并使用once()方法读取它。

您还可以使用Firebase管理SDK。

你应该看看有很多code samples


1
投票

我可能有点晚了,但我希望我的解决方案可以帮助一些人:

exports.processJob = functions.database.ref('/Jobs/{pushId}/client_job_complete').onWrite(event => {

    const status = event.data.val();

    return admin.database().ref('Jobs/' + event.params.pushId + '/other_job_complete').once('value').then((snap) => {

        const other = snap.val();
        console.log('Status', status, other);

        /** do something with your data here, for example increase its value by 5 */
        other = (other + 5);

        /** when finished with processing your data, return the value to the {{ admin.database().ref(); }} request */
        return snap.ref.set(other).catch((error) => {
            return console.error(error);
        }); 
    });
});

但请注意您的firebase数据库规则。

如果没有用户可以访问写Jobs/pushId/other_job_complete,除了您的云功能管理员,您需要使用可识别的,独特的uid初始化您的云功能管理员。

例如:

const functions         = require('firebase-functions');
const admin             = require('firebase-admin');
const adminCredentials  = require('path/to/admin/credentials.json');

admin.initializeApp({
    credential: admin.credential.cert(adminCredentials),
    databaseURL: "https://your-database-url-com",
    databaseAuthVariableOverride: {
        uid: 'super-special-unique-firebase-admin-uid'
    }
});

然后您的firebase数据库规则应如下所示:

"client_job_complete": {
    ".read": "auth !== null",
    ".write": "auth.uid === 'super-special-unique-firebase-admin-uid'"
}

希望能帮助到你!


-1
投票

你必须等待新ref上的once()的承诺,例如:

exports.processJob = functions.database.ref('/Jobs/{pushId}/client_job_complete')
  .onWrite(event => {

    const status = event.data.val();
    const ref = event.data.adminRef.root.child('Jobs/'+event.params.pushId+'/other_job_complete');
    ref.once('value').then(function(snap){
      const other = snap.val();
      console.log('Status', status, other);
      if(status && other) {
        return other;
      }
    });
  });

编辑修复@Doug史蒂文森注意到的错误(我确实说过“类似的东西”)

© www.soinside.com 2019 - 2024. All rights reserved.