Reputation: 175
My aim is to have a make a write to the database on a particular child node of each user. Below is my attempted cloud function:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref();
exports.sync = functions.https.onRequest((req, res) => {
ref.child('users').once('value')
.then(snap =>{
snap.forEach(childSnap => {
const selection = childSnap.val().selection;
const uid = childSnap.val().uid;
ref2 = admin.database().ref('users').child(uid).child('week1');
ref2.set(10);
res.send('It worked');
});
});
});
My issue is that the code keeps returning an error message :
Error: Firebase.child failed: First argument was an invalid path: "undefined".
Is something wrong with the code or is the issue with the HTTP request capability.
Below is the JSON of my users node :
{
"1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" : {
"email" : "[email protected]",
"fullname" : "Muyiz",
"selection" : [ 1, 2, 3, 4, 5, 6 ],
"teamname" : "Donawon",
"total" : 12,
"uid" : "1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" ,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 12,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"6K9rQiZQ3jaV38WWtDbNwxhqIwc2" : {
"email" : "[email protected]",
"fullname" : "Dami Etomi",
"selection" : 0,
"teamname" : "Bayern Neverlosin'",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"9OgN4HyMtARaQEQV1mKQ5lyE1992" : {
"email" : "[email protected]",
"fullname" : "Jonathan Majin",
"selection" : [ 40, 8, 10, 24, 18, 34 ],
"teamname" : "Chad fc",
"total" : 0,
"userName" : "[email protected]",
"uid": "9OgN4HyMtARaQEQV1mKQ5lyE1992",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 8,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"AGVZAUye5ZbZgvwCOpMeDkoOsEU2" : {
"email" : "[email protected]",
"fullname" : "Emeka Iheme",
"selection" : 0,
"teamname" : "Young Money",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"Dh3Pz0qfTuQoOJhVILtEOnhToeT2" : {
"email" : "[email protected]",
"fullname" : "Femi Awoniyi",
"selection" : [ 6, 33, 4, 14, 26, 50 ],
"teamname" : "Fab FC",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"EIppyMZlxZdis74yGKySU1YuEX53" : {
"email" : "[email protected]",
"fullname" : "Tayo Olasanoye",
"selection" : [ 3, 2, 1, 4, 5, 6 ],
"teamname" : "ITweetBangers",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"FBi91g1Ms4OpqzdXE0bh4HXlOGm1" : {
"email" : "[email protected]",
"fullname" : "Olumide Williams",
"selection" : [ 20, 4, 54, 11, 53, 26 ],
"teamname" : "Chad FC",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"KTpENH8F4GMdpYYJjdnEEvYAfsy2" : {
"email" : "[email protected]",
"fullname" : "Kola Ayanwale",
"selection" : [ 5, 49, 56, 21, 29, 8 ],
"teamname" : "Chef Olatimo",
"total" : 16,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 16,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"RptITpORoiQlXwLYVNlzX9KmRNW2" : {
"email" : "[email protected]",
"fullname" : "Lemachi Chris-Asoluka",
"selection" : [ 45, 40, 56, 18, 6, 9 ],
"teamname" : "Arsene' Nah",
"total" : 36,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 33,
"week7" : 0,
"week8" : 0,
"week9" : 3
},
"UMFabxncKoZ6XcHpPQYZHizJ7Yr1" : {
"email" : "[email protected]",
"fullname" : "Deji Awoniyi",
"selection" : [ 6, 40, 1, 15, 44, 11 ],
"teamname" : "Deji's troops",
"total" : 26,
"userName" : "[email protected]",
"week1" : 23,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 10,
"week8" : 13,
"week9" : 8
},
"WKfJhqpMiOdOQEJzKalL9JWhHR83" : {
"email" : "[email protected]",
"fullname" : "Ané Etomi",
"selection" : [ 4, 29, 31, 43, 41, 21 ],
"teamname" : "MAné Event",
"total" : 57,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"i5oe4taDt8dehsWsy5AXCCa4QTG3" : {
"email" : "[email protected]",
"fullname" : "Fisayo Atekoja",
"selection" : [ 14, 33, 4, 8, 40, 1 ],
"teamname" : "Special Juan FC",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"j8TYqi7toPP7cfy2y9go6PQbaNI2" : {
"email" : "[email protected]",
"fullname" : "err seven",
"selection" : 0,
"teamname" : "er",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"mjEUeytEydRmpMu2OIPHqfz0Ex03" : {
"email" : "[email protected]",
"fullname" : "El Tigre",
"selection" : [ 8, 19, 23, 44, 29, 9 ],
"teamname" : "FC OBUNIKWE",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"wdSm2CuC9WPPtDXoCcpwwcwrpU22" : {
"email" : "[email protected]",
"fullname" : "Bo",
"selection" : 0,
"teamname" : "Supadej",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
},
"zz9IVV597iZWxPbOpvNoFBpSqlf1" : {
"email" : "[email protected]",
"fullname" : "Nnamdi Ikeazor",
"selection" : [ 4, 5, 6, 29, 40, 41 ],
"teamname" : "Pogbance FC",
"total" : 0,
"userName" : "[email protected]",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
}
}
Upvotes: 1
Views: 734
Reputation: 317542
This value is likely coming back undefined:
const uid = childSnap.val().uid;
If it's undefined, that would cause this to fail because you passed an undefined value to child()
:
admin.database().ref('users').child(uid)
So, check to make sure you're getting the values you expect by logging them.
Upvotes: 1