使用来自 firebase 实时触发函数的其他参考值
Use other references value from firebase realtime trigger functions
嗨,我开始使用 firebase 函数向我的 android 应用程序添加一些后端逻辑我想使用一些不同于事件数据值的值
exports.makeUppercase = functions.database.ref('path')
.onWrite(event => {
const original = event.data.val();
//I want to use value like this.
const other_val= root.database.ref('path').val();
console.log('email', "firms", original);
const uppercase = original.toUpperCase();
return event.data.ref.parent.child('uppercase').set(uppercase);
});
我想使用 other_valwith 事件数据,但到目前为止我只使用事件数据引用,但我可以使用更多的父项或子项在数据库 event.data.ref 中上升或下降。我该如何解决?
编辑:
感谢@davidtaubmann 的回答,我尝试学习正确的方法。
exports.makeUppercase = functions.database.ref('/nonVerifiedUsers/{email}')
.onWrite(event => {
var changed= "";
// You can use ref parameter like this
var param=event.params.email;
admin.database().ref('/uppercase').once('value').then(function(snap){
changed=snap.val();
}).then(function(){
/*if you want to use changed value wait until it return and return where
you want using admin.database.ref and use event parameter in reference like
this*/
return admin.database.ref('/path/').child(param).set(changed);
/*You can return regular event data but make sure you didn't write under
same reference server count this again as event and loop until reach 32 th child(maxiumum child nodes)*/
return event.data.ref.parent.child('uppercase').set(changed);
});
//In addition you can return multiple times in one function:
return event.data.ref.parent.child('dif').set(changed);
});
使用 firebase-admin node_module
根据我上面的评论,我确实测试了这些选项,并且我认为,如果您从 node_modules 初始化 Firebase-admin(确保它也包含在 packages.json 中),您可以检索来自数据库的任何数据。
对于您的代码,我相信您只需在 index.js
文件的开头添加这两行(以及所有其他初始化):
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
然后您以后可以像通常使用 firebase
一样使用 admin
,像这样:
admin.database().ref('users/xxxx').once('value').then(function(snapshot){....
它非常适合我!
嗨,我开始使用 firebase 函数向我的 android 应用程序添加一些后端逻辑我想使用一些不同于事件数据值的值
exports.makeUppercase = functions.database.ref('path')
.onWrite(event => {
const original = event.data.val();
//I want to use value like this.
const other_val= root.database.ref('path').val();
console.log('email', "firms", original);
const uppercase = original.toUpperCase();
return event.data.ref.parent.child('uppercase').set(uppercase);
});
我想使用 other_valwith 事件数据,但到目前为止我只使用事件数据引用,但我可以使用更多的父项或子项在数据库 event.data.ref 中上升或下降。我该如何解决?
编辑: 感谢@davidtaubmann 的回答,我尝试学习正确的方法。
exports.makeUppercase = functions.database.ref('/nonVerifiedUsers/{email}')
.onWrite(event => {
var changed= "";
// You can use ref parameter like this
var param=event.params.email;
admin.database().ref('/uppercase').once('value').then(function(snap){
changed=snap.val();
}).then(function(){
/*if you want to use changed value wait until it return and return where
you want using admin.database.ref and use event parameter in reference like
this*/
return admin.database.ref('/path/').child(param).set(changed);
/*You can return regular event data but make sure you didn't write under
same reference server count this again as event and loop until reach 32 th child(maxiumum child nodes)*/
return event.data.ref.parent.child('uppercase').set(changed);
});
//In addition you can return multiple times in one function:
return event.data.ref.parent.child('dif').set(changed);
});
使用 firebase-admin node_module
根据我上面的评论,我确实测试了这些选项,并且我认为,如果您从 node_modules 初始化 Firebase-admin(确保它也包含在 packages.json 中),您可以检索来自数据库的任何数据。
对于您的代码,我相信您只需在 index.js
文件的开头添加这两行(以及所有其他初始化):
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
然后您以后可以像通常使用 firebase
一样使用 admin
,像这样:
admin.database().ref('users/xxxx').once('value').then(function(snapshot){....
它非常适合我!