使用 Cloud Functions 在 Firebase 中循环和添加数据
Looping and Adding data together in Firebase with Cloud Functions
我是一名 swift iOS 程序员,我对 firebase 函数和 node.js 的语言还很陌生。我有一个 firebase 数据库结构,可以在一个 "Gym" 中显示不同用户的运动细节。每个用户的锻炼都有行进的距离、以分钟为单位的时间和步速(以秒和分钟为单位)请参阅代表俱乐部的 json 代码。
{
"Gym" : {
"-KhnVNdA50oR04q073k5" : {
"distance" : 0.01,
"minutes" : 6,
"pace" : 7.5,
"seconds" : 40
},
"-KhqiOc0CGdFMXihGB2N" : {
"distance" : 3.54,
"minutes" : 5,
"pace" : 5.89,
"seconds" : 23
},
"-KhrfOsis4m51qM9kbl2" : {
"distance" : 3.90,
"minutes" : 2,
"pace" : 6.77,
"seconds" : 38
},
"-Ki7NswN2bQb0n1OIuyr" : {
"distance" : 4.53,
"minutes" : 10,
"pace" : 6.10,
"seconds" : 29
}
}
}
我想编写一个 firebase 函数(在 node.js 中)来循环遍历数据并将所有秒数加在一起 onWrite。从教程中我知道如何将信息记录到控制台等等。但我不知道如何遍历数据——这在 Swift 中非常容易。
这里是我的起点
exports.totalClubSeconds = function.database.ref('/ClubRuns/{RunID}').onWrite(event => {
const runID = event.params.RunID
});
非常感谢您的提前帮助。为空函数道歉。 Node.js 有点学习曲线,学习时间有点紧。 :(
event.data.forEach
将让您循环浏览快照。 Docs
试试这个:
const runID = event.params.RunID
let RunRef = admin.database().ref(`Gym/${RunID}`);
return RunRef.once('value').then(snapshot => {
if (snapshot.hasChildren()) {
const minutes= 0;
snapshot.forEach(function(child) {
//do the sum here
});
//return Promise here;
}
});
我是一名 swift iOS 程序员,我对 firebase 函数和 node.js 的语言还很陌生。我有一个 firebase 数据库结构,可以在一个 "Gym" 中显示不同用户的运动细节。每个用户的锻炼都有行进的距离、以分钟为单位的时间和步速(以秒和分钟为单位)请参阅代表俱乐部的 json 代码。
{
"Gym" : {
"-KhnVNdA50oR04q073k5" : {
"distance" : 0.01,
"minutes" : 6,
"pace" : 7.5,
"seconds" : 40
},
"-KhqiOc0CGdFMXihGB2N" : {
"distance" : 3.54,
"minutes" : 5,
"pace" : 5.89,
"seconds" : 23
},
"-KhrfOsis4m51qM9kbl2" : {
"distance" : 3.90,
"minutes" : 2,
"pace" : 6.77,
"seconds" : 38
},
"-Ki7NswN2bQb0n1OIuyr" : {
"distance" : 4.53,
"minutes" : 10,
"pace" : 6.10,
"seconds" : 29
}
}
}
我想编写一个 firebase 函数(在 node.js 中)来循环遍历数据并将所有秒数加在一起 onWrite。从教程中我知道如何将信息记录到控制台等等。但我不知道如何遍历数据——这在 Swift 中非常容易。
这里是我的起点
exports.totalClubSeconds = function.database.ref('/ClubRuns/{RunID}').onWrite(event => {
const runID = event.params.RunID
});
非常感谢您的提前帮助。为空函数道歉。 Node.js 有点学习曲线,学习时间有点紧。 :(
event.data.forEach
将让您循环浏览快照。 Docs
试试这个:
const runID = event.params.RunID
let RunRef = admin.database().ref(`Gym/${RunID}`);
return RunRef.once('value').then(snapshot => {
if (snapshot.hasChildren()) {
const minutes= 0;
snapshot.forEach(function(child) {
//do the sum here
});
//return Promise here;
}
});