Reputation: 3300
I'm currently trying to learn Firebase functions. This new feature seems to be pretty powerful and useful.
I'd like to use a function the catch a DB writing event, make a comparison with a specific location in DB and then write some data (depending on the comparison result) in another location in DB.
Here's my current Node.js code:
exports.verificaFineLavoro = functions.database.ref('/Aziende/{nomeazienda}/LogAsegno/{pushidbracciante}/{pushidlog}/Asegno')
.onWrite(event => {
const original = event.data.val();
console.log('VerificaFineLavoro', event.params.pushId, original);
const aSegno = original;
console.log('aSegno', aSegno +"");
const FineLavoro = ref.parent.parent.parent.parent.child("Asegno/"+aSegno+"/FineLavoro");
return event.data.ref.child('FL').set(FineLavoro);
});
Currently the function gets triggered but it stops working because of the references which are probably wrong.
Upvotes: 0
Views: 1928
Reputation: 600006
The Cloud Functions for Firebase documentation on triggering from the database contains code snippets that show how to access other nodes in the database.
For example, this snippet sets a value as a sibling to the node that triggered the function:
return event.data.ref.parent.child('uppercase').set(uppercase);
Here's another snippet from the sample on counting child nodes:
exports.countlikechange = functions.database.ref('/posts/{postid}/likes/{likeid}').onWrite(event => { const collectionRef = event.data.ref.parent; const countRef = collectionRef.parent.child('likes_count'); ...
To access the root of the database use event.data.ref.root
(to access the root node with the permissions of the user that triggered the function) or event.data.adminRef.root
(to access the root with full administrative permission).
Upvotes: 4