Reputation: 53
Hi I started add some backend logic to my android app using firebase functions I want to use some value different then event data value
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);
});
I want to use other_valwith event data but so far I just using event data ref but I can ascend or descend in database event.data.ref using more parent or child. How can I solve it?
Edit: I try and learn correct way to do this thanks to @davidtaubmann for his answer.
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);
});
Upvotes: 2
Views: 4416
Reputation: 3360
Following my comments above, I did tested the options and as thought, if you initialize the Firebase-admin from node_modules (be sure it is also included in packages.json) you can retrieve any data from the database.
For your code I believe you only have to add this 2 lines at the beginning of the index.js
file (with all the other initializations):
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
then you can later use the admin
as you would commonly use firebase
, like this:
admin.database().ref('users/xxxx').once('value').then(function(snapshot){....
It just works perfectly for me!
Upvotes: 4