如何在firebase函数中的一个子节点上的写触发器上从数据库中读取任何数据? [英] How to read any data from database on its write trigger on one of its child in firebase functions?
问题描述
我已经在 '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:
- 使用
event.data.ref.root
或event.data.adminRef.root
. - 使用大多数示例中已包含的 Admin SDK.
使用 event.data.ref.root
或 event.data.adminRef.root
您还可以使用event.data.ref.root
或event.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();
});
}
最后一种方法也适用于其他触发器类型,例如在 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屋!