0

I am new to Firebase cloud functions. I would like to have a function in index.js that trigger when a node in database is created - 1. it will take the params of the context 2. and find a value of a node, 3. get value from another database, 4. adding that value to 3 diff places.

exports.runningNumber = functions.database.ref("Main/Enemy/{enem_Id}/{event_Id}"}
.onCreate((snapshot, context) => {

var enemid = context.params.enem_Id;
var eventid = context.params.event_Id;

return admin.database().ref("Main/Enemy/" + enemid + "/SequenceNumber").once('value', (snapshot) => {

var newSeqNum = snapshot.val() + 1;

//then, get var userid through database().ref("Main/Enemy/" + enemid + "/" + eventid + "/user_Id")

//and then, use newSeqNum to:

//1. Replacing "Main/Enemy/" + enemid + "/SequenceNumber"

//2. Adding as string into "Main/Enemy/" + enemid + "/" + eventid + "/SeqNum"

//3. Adding as string into "Main/Users/" + userid + "/" + eventid + "/SeqNum"
});

});

Hope somebody can help on how to do this...

David García Bodego
  • 1,058
  • 3
  • 13
  • 21
daniel ng
  • 3
  • 3
  • Welcome to SO! There should be a minimum content in one question: Input sample (if needed), expected output sample (is needed), what you try, what you research... What did you try? SO is not a free coding service. – David García Bodego Nov 19 '19 at 03:53
  • Thanks for the advice. I am noob in js and the best I had done is to add newSeqNum in to the database as in no. 1 and 2 and then stuck:- `return admin.database().ref("Main/Enemy/" + enemid + "/SequenceNumber").set(newSeqNum).then((snapshot) => { var newSeqNumString = snapshot.val(); return admin.database().ref("Main/Enemy/" + enemid + "/" + eventid + "/SeqNum").set(newSeqNumString.toString()); }); });` – daniel ng Nov 19 '19 at 09:16
  • 1
    I had solved it with separate functions to avoid nesting problem. – daniel ng Nov 20 '19 at 04:08

0 Answers0