如何在firebase函数中的一个子项上的写入触发器上从数据库中读取任何数据? [英] How to read any data from database on its write trigger on one of its child in firebase functions?

查看:12
本文介绍了如何在firebase函数中的一个子项上的写入触发器上从数据库中读取任何数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在pojo"节点上放置了触发器,只要发生一些变化,它就会被触发.我可以访问它的子节点,但如何访问以下位置?

I have put trigger on 'pojo' node,whenever some change happens, it get triggered. I can access its child, but how do I access following location?

这个位置的价值:

/XVbW8ED5DmTUerEYZZykp1GN2TO2/deviceToken

/XVbW8ED5DmTUerEYZZykp1GN2TO2/deviceToken

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

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

exports.ok = functions.database.ref("/{uid}/messages/{key}/pojo")
   .onWrite(event => {
      var snap = event.data;
      var b = snap.child("admin").val();
   }

推荐答案

触发数据库函数时,Cloud Functions从触发点向下传入数据.要从数据库中的其他位置获取任何数据,您需要在代码中读取该数据.

When triggering a database function, Cloud Functions passes in the data from the point on which is was triggered downwards. To get any data from another location in the database, you need to read that data in your code.

所以如果你有一个兄弟admin:

So if you have a sibling admin:

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

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

exports.ok=functions.database.ref("/{uid}/messages/{key}/pojo")
.onWrite(event =>{

  var snap = event.data;
  return snap.ref.parent.child("admin").once("value").then((adminSnapshot) {
    var adminVal = adminSnapshot.val();
    // TODO: do something with the adminVal and return a value or a promise
  });

}

要从完全不相关的路径中读取值,您有两种选择:

To read a value from a completely unrelated path, you have two options:

  1. 使用 event.data.ref.rootevent.data.adminRef.root.
  2. 使用已包含在大多数示例中的 Admin SDK.

使用 event.data.ref.rootevent.data.adminRef.root

您还可以使用 event.data.ref.rootevent.data.adminRef.root 从根开始构建路径.这两个选项的区别在于 event.data.ref 与触发函数的用户具有相同的访问权限,而 event.data.adminRef 具有完全访问权限整个数据库.

Use event.data.ref.root or event.data.adminRef.root

You can also start building a path from the root, by using event.data.ref.root or event.data.adminRef.root. The difference between these two options is that event.data.ref has the same access permissions as the user that triggered the functions, while event.data.adminRef has full access to the entire database.

一个简单的例子是假设你有一个全局 admin 节点你想访问:

A simple example of this is say that you have a global admin node that you want to access:

exports.ok=functions.database.ref("/{uid}/messages/{key}/pojo")
.onWrite(event =>{

  var root = event.data.ref.root;
  return root.child("admin").once("value").then((adminSnapshot) {
    var adminVal = adminSnapshot.val();
    // TODO: do something with the adminVal and return a value or a promise
  });

}

使用已包含在大多数示例中的 Admin SDK.

或者,您可以从 Admin SDK 开始,它已包含在您的代码段中:

Use the Admin SDK, which is already included in most samples.

Alternatively you can start with the Admin SDK, which is already included in your snippet:

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

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

exports.ok=functions.database.ref("/{uid}/messages/{key}/pojo")
.onWrite(event =>{

  var snap = event.data;
  admin.database().ref('XVbW8ED5DmTUerEYZZykp1GN2TO2/deviceToken').once("value").then(function(tokenSnapshot) {
    var deviceToken = tokenSnapshot.val();
  });

}

另请参阅Firebase 文档的这一部分以获取另一个示例.

最后一种方法也适用于其他触发器类型,例如在 HTTP 触发函数中.由于这些函数不会获取 event.data.ref,因此 Admin SDK 是您唯一的选择.

This last approach also works with other trigger types, e.g. in HTTP triggered functions. Since these functions don't get event.data.ref, the Admin SDK is your only option here.

这篇关于如何在firebase函数中的一个子项上的写入触发器上从数据库中读取任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆