简体   繁体   中英

Cloud Functions for Firebase write operation inside a DB event function

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM