gpt4 book ai didi

javascript - 适用于 Firebase 的 Foreach 云函数

转载 作者:行者123 更新时间:2023-11-30 20:57:48 26 4
gpt4 key购买 nike

我的目标是在每个用户的特定子节点上对数据库进行写入。下面是我尝试的云函数:

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');
});
});


});

我的问题是代码不断返回错误消息:

Error: Firebase.child failed: First argument was an invalid path: "undefined".

是代码有问题还是HTTP请求能力有问题

以下是我的用户节点的 JSON:

{
"1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" : {
"email" : "muyiwaawoniyi@hotmail.com",
"fullname" : "Muyiz",
"selection" : [ 1, 2, 3, 4, 5, 6 ],
"teamname" : "Donawon",
"total" : 12,
"uid" : "1YrpX2W2xnMPoy4YGpZcOE0xJ5g2" ,
"userName" : "muyiwaawoniyi@hotmail.com",
"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" : "dami.etomi@gmail.com",
"fullname" : "Dami Etomi",
"selection" : 0,
"teamname" : "Bayern Neverlosin'",
"total" : 0,
"userName" : "dami.etomi@gmail.com",
"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" : "jonathan10majin@gmail.com",
"fullname" : "Jonathan Majin",
"selection" : [ 40, 8, 10, 24, 18, 34 ],
"teamname" : "Chad fc",
"total" : 0,
"userName" : "jonathan10majin@gmail.com",
"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" : "iheme.emeka@gmail.com",
"fullname" : "Emeka Iheme",
"selection" : 0,
"teamname" : "Young Money",
"total" : 0,
"userName" : "iheme.emeka@gmail.com",
"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" : "femi0911@yahoo.com",
"fullname" : "Femi Awoniyi",
"selection" : [ 6, 33, 4, 14, 26, 50 ],
"teamname" : "Fab FC",
"total" : 0,
"userName" : "femi0911@yahoo.com",
"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" : "tayomichael@hotmail.com",
"fullname" : "Tayo Olasanoye",
"selection" : [ 3, 2, 1, 4, 5, 6 ],
"teamname" : "ITweetBangers",
"total" : 0,
"userName" : "tayomichael@hotmail.com",
"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" : "olumidewilliams@hotmail.com",
"fullname" : "Olumide Williams",
"selection" : [ 20, 4, 54, 11, 53, 26 ],
"teamname" : "Chad FC",
"total" : 0,
"userName" : "olumidewilliams@hotmail.com",
"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" : "kmayanwale@yahoo.com",
"fullname" : "Kola Ayanwale",
"selection" : [ 5, 49, 56, 21, 29, 8 ],
"teamname" : "Chef Olatimo",
"total" : 16,
"userName" : "kmayanwale@yahoo.com",
"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" : "lemarchris@yahoo.co.uk",
"fullname" : "Lemachi Chris-Asoluka",
"selection" : [ 45, 40, 56, 18, 6, 9 ],
"teamname" : "Arsene' Nah",
"total" : 36,
"userName" : "lemarchris@yahoo.co.uk",
"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" : "awoniyideji@yahoo.com",
"fullname" : "Deji Awoniyi",
"selection" : [ 6, 40, 1, 15, 44, 11 ],
"teamname" : "Deji's troops",
"total" : 26,
"userName" : "awoniyideji@yahoo.com",
"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" : "ane.etomi@gmail.com",
"fullname" : "Ané Etomi",
"selection" : [ 4, 29, 31, 43, 41, 21 ],
"teamname" : "MAné Event",
"total" : 57,
"userName" : "ane.etomi@gmail.com",
"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" : "fatekoja@gmail.com",
"fullname" : "Fisayo Atekoja",
"selection" : [ 14, 33, 4, 8, 40, 1 ],
"teamname" : "Special Juan FC",
"total" : 0,
"userName" : "fatekoja@gmail.com",
"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" : "r7@yahoo.com",
"fullname" : "err seven",
"selection" : 0,
"teamname" : "er",
"total" : 0,
"userName" : "r7@yahoo.com",
"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" : "jaiyeola03@hotmail.co.uk",
"fullname" : "El Tigre",
"selection" : [ 8, 19, 23, 44, 29, 9 ],
"teamname" : "FC OBUNIKWE",
"total" : 0,
"userName" : "jaiyeola03@hotmail.co.uk",
"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" : "foogunlana@yahoo.com",
"fullname" : "Bo",
"selection" : 0,
"teamname" : "Supadej",
"total" : 0,
"userName" : "foogunlana@yahoo.com",
"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" : "nnamdi.ikeazor@yahoo.com",
"fullname" : "Nnamdi Ikeazor",
"selection" : [ 4, 5, 6, 29, 40, 41 ],
"teamname" : "Pogbance FC",
"total" : 0,
"userName" : "nnamdi.ikeazor@yahoo.com",
"week1" : 0,
"week10" : 0,
"week11" : 0,
"week12" : 0,
"week2" : 0,
"week3" : 0,
"week4" : 0,
"week5" : 0,
"week6" : 0,
"week7" : 0,
"week8" : 0,
"week9" : 0
}
}

最佳答案

这个值可能会返回未定义的:

const uid = childSnap.val().uid;

如果它是未定义的,那将导致失败,因为您将未定义的值传递给 child():

admin.database().ref('users').child(uid)

因此,检查以确保通过记录它们获得了预期的值。

关于javascript - 适用于 Firebase 的 Foreach 云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com