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

How to get database value not related to the event in Cloud Functions for Firebase?

我有一个 firebase 数据库,我目前正在尝试使用云函数在我的数据库中的值发生变化时执行操作。到目前为止,当我的数据库中的值发生变化时,它成功地将代码触发到 运行 。但是,当数据库值发生变化时,我现在需要检查另一个值以确定它的状态,然后再执行一个操作。问题是我有 ~0 的 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 但它只谈论事件并且非常简短

感谢您的帮助!

您必须等待新 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 注意到的错误(我确实说过 "something like")

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

event.data.ref
event.data.adminRef

ref is limited to the permissions of the user who triggered the function. adminRef 具有对数据库的完全访问权限。

每个 Reference objects has a root property which gives you a reference to the root of your database. You can use that reference to build a path to a reference in another part of your database, and read it with the once() 方法。

您还可以使用 Firebase admin SDK。

还有很多 code samples 您可能也应该看看。

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

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'"
}

希望对您有所帮助!