Javascript 如何在Firebase的云函数中获取与事件无关的数据库值?

Javascript 如何在Firebase的云函数中获取与事件无关的数据库值?,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我有一个firebase数据库,目前正试图在数据库中的值发生变化时使用云函数执行操作。到目前为止,当数据库中的值发生更改时,它成功地触发代码运行。但是,当数据库值更改时,我现在需要检查另一个值以确定它的状态,然后执行一个操作。问题是,我对JS有~0的经验,除了部署、更改数据库中的值和查看控制台日志之外,我没有办法调试代码 有没有办法在数据库中查找另一个值并读取它?查一个值,然后为它设置一个值怎么样?代码如下: exports.determineCompletion= functions.data

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

有没有办法在数据库中查找另一个值并读取它?查一个值,然后为它设置一个值怎么样?代码如下:

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关联的值,并将其存储为status。但是我如何获得其他值呢

此外,如果任何人有任何JS或firebase文档,他们认为可以帮助我,请分享!我在这里读过一些关于firebase的文章:但它只讨论事件,而且非常简短


谢谢你的帮助

您必须等待新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 Stevenson注意到的错误(我确实说过“类似”)

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

event.data.ref
event.data.adminRef
仅限于触发该功能的用户的权限。具有对数据库的完全访问权限

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

您还可以使用Firebase管理SDK


您可能还需要了解很多信息。

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

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\u job\u 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'"
}

希望有帮助

当我尝试这样做时,我得到了错误“ref.once不是一个函数”。知道为什么会这样吗?谢谢你的帮助,但这行不通-你不能像那样使用functions.database.ref。这太棒了。这些代码示例非常有用。这就是我要找的。请使用.adminRef或.ref@AlxVallejo