Firebase Functions (trigger) "dynamically" write / update Firebase node
I want to get data from Firebase Node Games / {gid} / BoxScores which has four child nodes: Q1, Q2, Q3, Q4
Below is the code I am trying to implement; where qtrDesc is a link to any of these child nodes in BoxScores:Q1 or Q2 or Q3 or Q4
exports.boxScoresUpdate = functions.database.ref('/Games/{gid}/BoxScores').onWrite(event => {
event.data.ref.parent.child('Qtr').once('value', data => {
console.log('Qtr is: ', data.val());
for(let i = data.val(); i > 0; i--) {
let qtrDesc = "Q".concat(i);
console.log('qtrDesc: ', qtrDesc); // Outputs the desired 'Q1, Q2, Q3, Q4'
// Outputs is "undefined" in the function logs
console.log('Qtr Score is: ', newValue.qtrDesc); // ****Does not reference the desired child node
}
});
});
The problem with the above code is the output in the logs on the Firebase function dashboard undefined
as it tries to get newValue
for qtrDesc
(which doesn't exist) instead of newValue
for Q1 or Q2 or Q3 or Q4
as desired !?
json Node:
"Games": {
"12345": {
"Date": "20170819",
"BoxScores": {
"Q1": "10",
"Q2": "21",
"Q3": "90",
"Q4": "100"
},
"Qtr": "4"
}
}
source to share
It's hard to be sure without seeing your data structure. But I think what you are looking for is:
event.data.ref.parent.child('Qtr').once('value', data => {
data.forEach(quarterSnap => {
let qtrDesc = quarterSnap.key;
let qtrVal = quarterSnap.val();
console.log(qtrDesc, qtrVal);
}
});
If this is not what you need, edit your question to include a snippet of the JSON structure you are using (as text, no screenshots). You can get this by clicking the Export JSON link in the Firebase Database Console .
source to share